使用grepl重置列

时间:2017-04-06 00:11:02

标签: r data-cleaning grepl

我正在尝试重写一些内容,例如:

数据$程序[%c中的数据$程序%('A1','1A1','A','AA','11A')]< - ''A'

我想使用与SQL相同的%A%重写这行代码。我相信使用grepl函数有一种方法可以做到这一点,但我无法弄清楚如何

1 个答案:

答案 0 :(得分:1)

你几乎就在那里。 grepl的简单应用就可以实现。

Data$Program[grepl(Data$Program,'A')] <- 'A'

例如使用虹膜数据集:

myIris<-iris
myIris$Species <- as.character(myIris$Species)
myIris$Species[grepl('i',myIris$Species)] <- "I"
myIris$Species
  [1] "setosa" "setosa" "setosa" "setosa" "setosa" "setosa" "setosa" "setosa"
  [9] "setosa" "setosa" "setosa" "setosa" "setosa" "setosa" "setosa" "setosa"
 [17] "setosa" "setosa" "setosa" "setosa" "setosa" "setosa" "setosa" "setosa"
 [25] "setosa" "setosa" "setosa" "setosa" "setosa" "setosa" "setosa" "setosa"
 [33] "setosa" "setosa" "setosa" "setosa" "setosa" "setosa" "setosa" "setosa"
 [41] "setosa" "setosa" "setosa" "setosa" "setosa" "setosa" "setosa" "setosa"
 [49] "setosa" "setosa" "I"      "I"      "I"      "I"      "I"      "I"     
 [57] "I"      "I"      "I"      "I"      "I"      "I"      "I"      "I"     
 [65] "I"      "I"      "I"      "I"      "I"      "I"      "I"      "I"     
 [73] "I"      "I"      "I"      "I"      "I"      "I"      "I"      "I"     
 [81] "I"      "I"      "I"      "I"      "I"      "I"      "I"      "I"     
 [89] "I"      "I"      "I"      "I"      "I"      "I"      "I"      "I"     
 [97] "I"      "I"      "I"      "I"      "I"      "I"      "I"      "I"     
[105] "I"      "I"      "I"      "I"      "I"      "I"      "I"      "I"     
[113] "I"      "I"      "I"      "I"      "I"      "I"      "I"      "I"     
[121] "I"      "I"      "I"      "I"      "I"      "I"      "I"      "I"     
[129] "I"      "I"      "I"      "I"      "I"      "I"      "I"      "I"     
[137] "I"      "I"      "I"      "I"      "I"      "I"      "I"      "I"     
[145] "I"      "I"      "I"      "I"      "I"      "I"