I'm very, very new to R, and I have some data from quiz scores that is stored as strings like "2.00 / 2" or "0.00 / 2," and I'm trying to turn them into numeric types so that I can do operations on them. How does one apply a functions like gsub() and as.numeric() to all the data (except the first column, which has participants' names)?
            Asked
            
        
        
            Active
            
        
            Viewed 38 times
        
    1
            
            
        - 
                    `df[-1] <- lapply(df[-1], as.numeric)` – Sotos Jun 15 '21 at 13:58
 - 
                    2It would be easier to help if you create a small reproducible example along with expected output. Read about [how to give a reproducible example](http://stackoverflow.com/questions/5963269). – Ronak Shah Jun 15 '21 at 14:00
 
2 Answers
0
            
            
        You can see how to evaluate text expressions here Evaluate expression given as a string.
In your case I would first create a function to evaluate a vector and then define which vectors to evaluate using across().
library(tidyverse)
example_df <- data.frame(
  name = c("John", "David"),
  answer1 = c("2 / 5", "10 / 2"),
  answer2 = c("1 / 3", "5 + 4")
)
evaluate_expression <- function(vec){
  vec %>% 
    map(~ parse(text = .)) %>% 
    map_dbl(eval)
}
example_df %>% 
  mutate(across(c(answer1, answer2), evaluate_expression))
#    name answer1   answer2
# 1  John     0.4 0.3333333
# 2 David     5.0 9.0000000
        yogevmh
        
- 316
 - 1
 - 5
 
- 
                    Thanks so much! I've added the following to my code: `format_col <- function(col){ col %>% map(~ parse(1,text = .)) %>% map_dbl(eval) } pre = pre %>% transmute(Nombre=Nombre, across(starts_with("X"),format_col))` but I'm still getting this error: Error: Problem with `mutate()` input `..2`. i `..2 = across(starts_with("X"), format_col)`. x Result 28 must be a single double, not NULL of length 0 sorry this is so messy - is there a way to do formatting in comments on this thing? – ezrudel Jun 16 '21 at 17:18
 
0
            My friend helped me out and I eventually got it to work like this:
format_col = function(var){
  as.numeric(str_sub(var,1,1)) /
    as.numeric(str_sub(var,8,8))
}
df = df %>% transmute(Nombre=Nombre,
                      across(starts_with("X"),
                             format_col))
        ezrudel
        
- 13
 - 2