替换R中数据框列中的数字?

时间:2012-08-05 14:50:10

标签: r replace numbers dataframe

  

可能重复:
  Replace contents of factor column in R dataframe

我有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

我想用df1 $ Sp2列中的数字8的每个条目替换为数字800.我试过:

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

1 个答案:

答案 0 :(得分:44)

e.g:

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