Replace numbers in data frame column in R? [duplicate]

心不动则不痛 提交于 2019-11-28 02:34:04

问题


Possible Duplicate:
Replace contents of factor column in R dataframe

I have the data.frame

df1<-data.frame("Sp1"=1:6,"Sp2"=7:12,"Sp3"=13:18)
rownames(df1)=c("A","B","C","D","E","F")

df1
  Sp1 Sp2 Sp3
A   1   7  13
B   2   8  14
C   3   9  15
D   4  10  16
E   5  11  17
F   6  12  18

I want to replace every entry of the number 8 in the column df1$Sp2 with the number 800. I have tried:

test<-replace(df1$Sp2,df1[800,"Sp2"],5)

回答1:


e.g.:

df1$Sp2[df1$Sp2 == 8] <- 800


来源:https://stackoverflow.com/questions/11817371/replace-numbers-in-data-frame-column-in-r

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!