Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我有一个包含字符串列表的数据框
df$a ========= "4343-2" "7889-5" "4-3456" "334-45" "8765-4"
我想对列表执行字符串操作以删除破折号,所以我这样做了..
df$a <- lapply(df$a, sub, "-","", df$a)
..它只产生一组完全空的字符串。我做错了什么?
你可以直接使用sub。
sub
df$a <- sub('-', '', df$a)
lapply因为 sub 是“矢量化的”,而不是你正在做的复杂的事情。gsub如果您认为每个条目可能有多个破折号,也可以使用。
lapply
gsub