我有一个dataframe列,如下所示:
name_col
"Anna"
"c(\"Anna\", \n\"Billy\", \n\"Fanny\")"
"c(\"Tom\", \"Tim\"\n)"
"Minnie"
我想获取以上列中每一行的最后一个字符串, 像这样的东西:
name_col
"Anna"
"Fanny"
"Tim"
"Minnie"
dput(head(name_col,4))看起来像这样:
c("Anna","c(\"Anna\", \"Billy\", \"Fanny\")" ,"c(\"Tom\", \"Tim\")","Minnie")
请让我知道是否可以在R中的dataframe列上完成。谢谢
答案 0 :(得分:1)
使用末尾注释中可重复显示的数据帧if
,请使用以下替换:
d
给予:
transform(d, name_col = sub('.*"(.*)".*', "\\1", name_col))
可重复显示的输入数据框:
name_col
1 Anna
2 Fanny
3 Tim
4 Minnie