> df <- data.frame(x=c('a-b','c-d','e-f'),y=c('[1-2]','(3-4)','[5-6)'),stringsAsFactors=F)
> df
x y
1 a-b [1-2]
2 c-d (3-4)
3 e-f [5-6)我尝试使用gsub()扫描df的所有列,但只用字母表中的列中的"-"替换".",使其看起来像这样:
> df
x y
1 a.b [1-2]
2 c.d (3-4)
3 e.f [5-6)使用sapply(names(df), function(x) gsub("\\-", ".", df[, x]))不起作用:
> sapply(names(df), function(x) gsub("\\-", ".", df[, x]))
x y
[1,] "a.b" "[1.2]"
[2,] "c.d" "(3.4)"
[3,] "e.f" "[5.6)"有没有更好的方法来做到这一点,要么使用gsub(),要么使用像make.names()这样可以选择列的工具?
谢谢
https://stackoverflow.com/questions/47776543
复制相似问题