【发布时间】:2014-03-10 22:29:29
【问题描述】:
我想将 data.frame 中的几列从 chr 转换为数字,我想在一行中完成。这是我想要做的:
items[,2:4] <- as.numeric(sub("\\$","",items[,2:4]))
但我收到一条错误消息:
Warning message:
NAs introduced by coercion
如果我一栏一栏地做,虽然它有效:
items[,2:2] <- as.numeric(sub("\\$","",items[,2:2]))
items[,3:3] <- as.numeric(sub("\\$","",items[,3:3]))
items[,4:4] <- as.numeric(sub("\\$","",items[,4:4]))
我在这里缺少什么?为什么我为多个列指定此命令?这是我不知道的一些奇怪的 R 特质吗?
示例数据:
Name, Cost1, Cost2, Cost3, Cost4
A, $10.00, $15.50, $13.20, $45.45
B, $45.23, $34.23, $34.24, $23.34
C, $23.43, $45.23, $65.23, $34.23
D, $76.34, $98.34, $90.34, $45.09
【问题讨论】:
标签: r dataframe type-conversion