Replace characters from a column of a data frame R

后端 未结 4 2095
别那么骄傲
别那么骄傲 2020-12-13 04:00

I have a data frame

a <- runif (10)
b <- letters [1:10]
c <- c(rep (\"A-B\", 4), rep(\"A_C\", 6))
data1 <- data.frame (a, b, c)
data1

4条回答
  •  渐次进展
    2020-12-13 04:47

    chartr is also convenient for these types of substitutions:

    chartr("_", "-", data1$c)
    #  [1] "A-B" "A-B" "A-B" "A-B" "A-C" "A-C" "A-C" "A-C" "A-C" "A-C"
    

    Thus, you can just do:

    data1$c <- chartr("_", "-", data1$c)
    

提交回复
热议问题