consider the following code
x <- c('2','75% (3/4)','80% (4/5)','70% (7/10)','90% (9/10)')
y <- c('1', '50% (1/2)', '25% (1/4)', '30% (3/10)', '40% (2/5)')
df <- data.frame(rbind(x, y))
I would like to extract the values before the % sign i.e. the whole numbers.
I understand that I can do this using the following:
df$X2 <- sub("%.*", "", df$X2)
But to avoid copy and pasting, and going through each column, is there a way to do it in one step?
I have tried to do the following:
df[-1] <- sub("%.*", "", df[-1])
But this leaves the format as 'c("75' which is not what I am after - what has gone wrong here? Is there another suitable way to do this?
Thanks