用R中的数据帧中的31替换31-33

时间:2017-03-22 15:11:05

标签: r if-statement replace conditional substitution

如果我在R中有数据框df:

area industry value
1       31-33   6
2       44-45   1023
3       48-49    8

我如何将31-44替换为31,44-45替换为44,将48-49替换为48? 我尝试过的这个网站上的每个例子都没有效果。 我的最新尝试是

levels(df$industry)[levels(df$industry)=="31-33"] <- "31"

但是就像我尝试的其他所有内容一样,一旦我将数据写入txt文件并将其导入SQL,它就会显示为null。

请记住,除了这三个行之外,还有更多列和大量行,其中包含更多行业代码,但这三个行是唯一需要更改的行。 感谢。

1 个答案:

答案 0 :(得分:1)

您可以尝试使用sub功能

df$industry<-sub("31-33","31",df$industry)