使用gsub删除R中第一个空格之前的所有字符串

时间:2022-11-17 06:09:28

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 white space in the name column. Can anybody give me a favor? Thank you!

我想删除名称列中第一个空格之前的所有字符。谁有人帮我一个忙?谢谢!

3 个解决方案

#1


7  

Try this:

尝试这个:

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

#2


6  

If D is your data frame, try

如果D是您的数据框,请尝试

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

#3


1  

Let's say your data frame is called 'df'

假设您的数据框称为'df'

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

#1


7  

Try this:

尝试这个:

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

#2


6  

If D is your data frame, try

如果D是您的数据框,请尝试

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

#3


1  

Let's say your data frame is called 'df'

假设您的数据框称为'df'

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