Danielle
Danielle

Reputation: 795

Make new column that contains part of expression in another column

I have a data frame like so:

 before<- data.frame( Var1= 
  c("174_1","174_1","174_2","174_3","175_1","175_1"))

I would like to add another column Var2 that contains the part of the expression in Var1 before the underscore. The new column would appear as follows:

after<- data.frame( Var1= 
  c("174_1","174_1","174_2","174_3","175_1","175_1"), Var2= 
  c("174","174","174","174","175","175"))

I am believe functions like grepl() could be useful for this, however, I do not know how to specify keeping part of an before the grepl("_").

Upvotes: 1

Views: 60

Answers (3)

3pitt
3pitt

Reputation: 941

df1$b <- substr(df1$a, 1, regexpr('_', df1$a)[1]-1)

This takes a substring of everything up until the underscore

Upvotes: 1

Albert Simmons
Albert Simmons

Reputation: 176

before <- data.frame(Var1= c("174_1","174_1","174_2","174_3","175_1","175_1"))

after <- data.frame(Var1 = before$Var1,Var2 = unlist(lapply(strsplit(as.character(before$Var1), '_'), `[[`,1)))

Upvotes: 1

kgolyaev
kgolyaev

Reputation: 565

Use tidyr::separate:

d = data.frame(Var1 = c("174_1","174_1","174_2","174_3","175_1","175_1"))
temp = tidyr::separate(d, Var1, into=c("v1", "v2"), sep="_")
temp
   v1 v2
1 174  1
2 174  1
3 174  2
4 174  3
5 175  1
6 175  1
d[["Var2"]] <- temp[["v1"]]

Upvotes: 1

Related Questions