3

给定这样的数据框:

 DL<-c("Dark52","Light-298","dark7","23_dark","The_light","Lights","77dark","9Light")
 Col1<-c(1,12,3,6,4,8,2,8)
 DF<-data.frame(Col1)
 row.names(DF)<-DL

如何创建包含行名称的“暗”或“亮”元素的第二列?

所以最终结果看起来像这样

Col2<-c("Dark","Light","dark","dark","light","Light","dark","Light")
DF$Col2<-Col2


          Col1  Col2
Dark52       1  Dark
Light-298   12 Light
dark7        3  dark
23_dark      6  dark
The_light    4 light
Lights       8 Light
77dark       2  dark
9Light       8 Light

4

2 回答 2

3

使用正则表达式:

x <- gsub("[^a-zA-Z]", "", row.names(DF))
DF$Col2 <- "dark"
DF$Col2[agrep("light", x)] <- "light"

          Col1  Col2
Dark52       1  dark
Light-298   12 light
dark7        3  dark
23_dark      6  dark
The_light    4 light
Lights       8 light
77dark       2  dark
9Light       8 light

PS:本来打算做,gsub("[^dark|light]", "", row.names(DF), ignore.case = TRUE)但它失败了The_light

于 2013-07-27T21:55:15.717 回答
3

这是一种方法,但也许有更直接的方法:D

> transform(DF, 
+           Col2=sapply(strsplit(tolower(gsub("[0-9]", "", rownames(DF))), "[[:punct:]]"), 
+                       function(x) x[x%in% c("dark", "light", "lights")]))
          Col1   Col2
Dark52       1   dark
Light-298   12  light
dark7        3   dark
23_dark      6   dark
The_light    4  light
Lights       8 lights
77dark       2   dark
9Light       8  light
于 2013-07-27T22:28:56.837 回答