-2

I have a data frame DF in which few columns are having comma and character values in it. I want to remove comma and replace character values with blank. But want to retain col5 as it is. I should convert only col1 to col4 to numeric and remove comma.

col1=c(1,45,30,35)
col2=c(33,"33,345",87,89)
col3=c("67,345",77,90,"reply")
col4=c("silver",43,23,55)
col5=c("gb","rt","dc","ty")

DF=data.frame(col1,col2,col3,col4,col5)

col6=c(1,45,30,35)
col7=c(33,33345,87,89)
col8=c(67345,77,90,"")
col9=c("",43,23,55)
col10=c("gb","rt","dc","ty")

DF_Output=data.frame(col6,col7,col8,col9,col10)

My expected output is like DF_Output. Please help me to solve this.

  • In `col8` and `col9` you want to have empty string `""` and numeric number to be together which is not possible in R, col8 and col9 would be coerced to character vector for those `""`. – shafee Jul 07 '22 at 05:41
  • I can have NA also in place of blank. Like the way Park did it. – ParthaSarathi Jul 07 '22 at 07:20

2 Answers2

1

You may try

library(dplyr)
library(stringr)

DF %>%
  mutate(across(col1:col4, ~as.numeric(str_remove(.x, ","))))
  col1  col2  col3 col4 col5
1    1    33 67345   NA   gb
2   45 33345    77   43   rt
3   30    87    90   23   dc
4   35    89    NA   55   ty
Park
  • 14,771
  • 6
  • 10
  • 29
0

You can use parse_number from readrpackage as well:

cbind(apply(DF[1:4], 2, readr::parse_number),
      DF[5]
)

#   col1  col2  col3 col4 col5
# 1    1    33 67345   NA   gb
# 2   45 33345    77   43   rt
# 3   30    87    90   23   dc
# 4   35    89    NA   55   ty
AlexB
  • 3,061
  • 2
  • 17
  • 19