简体   繁体   中英

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("_").

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

这会占用所有内容的子字符串直到下划线

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"]]
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)))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM