Use gsub to delete the whole line before the first space in R

I have a data frame like this:

name weight r apple 0.5 y pear 0.4 y cherry 0.1 g watermelon 5.0 pp grape 0.5 y apple pear 0.4 ... ... 

I would like to remove all characters before the first space in the name column. Can someone do me a favor? Thanks!

+5
source share
3 answers

Try the following:

 sub(".*? (.+)", "\\1", D$name) 
+5
source

If D is your data frame, try

 sub(".+? ", "", D$name) 
+5
source

Say your data frame is called 'df'

 library(reshape2) df$name = colsplit(df$name," ", names = c("chuck","name"))[,2] 
+1
source

Source: https://habr.com/ru/post/1232206/


All Articles