Replace characters from a column of a data frame R

后端 未结 4 2086
别那么骄傲
别那么骄傲 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:30

    If your variable data1$c is a factor, it's more efficient to change the labels of the factor levels than to create a new vector of characters:

    levels(data1$c) <- sub("_", "-", levels(data1$c))
    
    
                a b   c
    1  0.73945260 a A-B
    2  0.75998815 b A-B
    3  0.19576725 c A-B
    4  0.85932140 d A-B
    5  0.80717115 e A-C
    6  0.09101492 f A-C
    7  0.10183586 g A-C
    8  0.97742424 h A-C
    9  0.21364521 i A-C
    10 0.02389782 j A-C
    

提交回复
热议问题