简体   繁体   English

将“ *”移动到R中的新列

[英]Move “*” to new column in R

Hello I have a column in a data.frame, it has many rows, eg, 您好,我在data.frame中有一列,它有很多行,例如,

df = data.frame("Species" = c("*Briza minor", "*Briza minor", "Wattle"))

I want to make a new column "Species_new" where the "*" is moved to the end of the character string, eg, 我想创建一个新列“ Species_new”,其中“ *”移到字符串的末尾,例如,

df = data.frame("Species" = c("*Briza minor", "*Briza minor", "Wattle"),
            "Species_new" = c("Briza minor*", "Briza minor*", "Wattle"))

Is there a way to do this using gsub? 有没有办法使用gsub做到这一点? The manual example would take far too long as I have approximately 50,000 rows. 手动示例将花费太长时间,因为我大约有50,000行。

Thanks in advance 提前致谢

One option is to capture the * as a group and in the replacement reverse the backreferences 一种选择是将*捕获为一个组,并在replacement反向引用

df$Species_new <- sub("^([*])(.*)$", "\\2\\1", df$Species)
df$Species_new
#[1] "Briza minor*" "Briza minor*" "Wattle"    

NOTE: * is a metacharacter meaning 0 or more, so we can either escape ( \\\\* ) or place it in brackets ( [] ) to evaluate the raw character ie literal evaluation 注意: *是一个元字符,表示0或更大,因此我们可以转义( \\\\* )或将其放在方括号( [] )中以评估原始字符,即文字评估

Thanks so much for the quick response, I also found a workaround; 非常感谢您的快速回复,我也找到了一种解决方法。

df$Species_new = sub("[*]","",df$Species, perl=TRUE)

differences = setdiff(df$Species,df$Species_new)

tochange = subset(df,df$Species == differences)
toleave = subset(df,!df$Species == differences)

tochange$Species_new = paste(tochange$Species_new, "*", sep = "")

df = rbind(tochange,toleave)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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