在 R 中使用 gsub 删除第一个白色 space 之前的所有字符串
Use gsub remove all string before first white space in R
我有一个这样的数据框:
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
... ...
我想删除名称列中第一个白色 space 之前的所有字符。有人可以帮我一个忙吗?谢谢!
如果 D
是您的数据框,请尝试
sub(".+? ", "", D$name)
试试这个:
sub(".*? ", "", D$name)
编辑:
该模式正在寻找任何字符零次或多次 (.*
) 直到第一个 space,然后捕获之后的一个或多个字符 ((.+)
)首先space。 .*
之后的 ?
使它成为 "lazy" 而不是 "greedy" 并且它在找到第一个 space 时停止。因此,.*?
匹配第一个 space 之前的所有内容,space 匹配找到的第一个 space。
假设您的数据框名为 'df'
library(reshape2)
df$name = colsplit(df$name," ", names = c("chuck","name"))[,2]
以下解决方案不使用 gsub,但可以使用管道运算符将其应用于数据帧 %>%
。
library(tidyverse)
# The data
df <- structure(list(name = c("r apple", "y pear", "y cherry", "g watermelon",
"pp grape", "y apple pear"), weight = c(0.5, 0.4, 0.1, 5.0, 0.5, 0.4)),
class = "data.frame", row.names = c(NA, -6L))
# Remove the first characters preceding a white space in the column "name"
df2 <- df %>%
mutate(name = str_replace(name, "^\S* ", ""))
正则表达式"^\S* "
搜索从字符串开头直到第一个白色的所有字符space。
我有一个这样的数据框:
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
... ...
我想删除名称列中第一个白色 space 之前的所有字符。有人可以帮我一个忙吗?谢谢!
如果 D
是您的数据框,请尝试
sub(".+? ", "", D$name)
试试这个:
sub(".*? ", "", D$name)
编辑:
该模式正在寻找任何字符零次或多次 (.*
) 直到第一个 space,然后捕获之后的一个或多个字符 ((.+)
)首先space。 .*
之后的 ?
使它成为 "lazy" 而不是 "greedy" 并且它在找到第一个 space 时停止。因此,.*?
匹配第一个 space 之前的所有内容,space 匹配找到的第一个 space。
假设您的数据框名为 'df'
library(reshape2)
df$name = colsplit(df$name," ", names = c("chuck","name"))[,2]
以下解决方案不使用 gsub,但可以使用管道运算符将其应用于数据帧 %>%
。
library(tidyverse)
# The data
df <- structure(list(name = c("r apple", "y pear", "y cherry", "g watermelon",
"pp grape", "y apple pear"), weight = c(0.5, 0.4, 0.1, 5.0, 0.5, 0.4)),
class = "data.frame", row.names = c(NA, -6L))
# Remove the first characters preceding a white space in the column "name"
df2 <- df %>%
mutate(name = str_replace(name, "^\S* ", ""))
正则表达式"^\S* "
搜索从字符串开头直到第一个白色的所有字符space。