Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating a column from word elements of separate column

Tags:

regex

r

Given a data frame like this:

 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

How can I create a second column containing either the "Dark" or "Light" elements of the row names?

So the end result would look like this

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

like image 340
Vinterwoo Avatar asked Nov 19 '25 18:11

Vinterwoo


2 Answers

Using a regex:

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: Was going to do gsub("[^dark|light]", "", row.names(DF), ignore.case = TRUE) but it fails for The_light.

like image 167
Tyler Rinker Avatar answered Nov 21 '25 08:11

Tyler Rinker


Here's one approach, but maybe there's a more direct way to do it :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
like image 27
Jilber Urbina Avatar answered Nov 21 '25 07:11

Jilber Urbina



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!