0
votes

Consider the str of dataframe, example, below:

 $ A                : num 0.00606
 $ B                : num 0.00466
 $ C                : num 0.00746
 $ D    : Factor w/ 2 levels "-0.04","D": 1
  ..- attr(*, "names")= chr "Tilt"
 $ E   : Factor w/ 2 levels " 0.00","E": 1
  ..- attr(*, "names")= chr "Tilt"

How do I convert it such that it becomes:

 $ A                : num 0.00606
 $ B                : num 0.00466
 $ C                : num 0.00746
 $ D                : num -0.04
 $ E                : num -0.00
df$D <- as.numeric(as.character(df$D)))Adam Quek
Thanks Adam - is there any way to not need to specify the column that needs to be converted, but instead automatically convert those that are factor?Westwinter1
Could you please dput() your dataframe?TarJae
Try this: df[ sapply(df, is.factor)] <- lapply(df[ sapply(df, is.factor)], function(x) as.numeric(as.character(x)))TarJae