【发布时间】:2019-12-18 13:23:06
【问题描述】:
我在数据框中有一个变量,其中包含的值要么是 Varies with device,要么是 a number with M,要么是 a number with k。
例如:
10M
1.2M
120k
Varies with device
我想使用the values with M 并简单地删除M,我想使用the values with k 并删除k 并将它们乘以0.001。
示例输出:
10
1.2
0.12
Varies with device
我希望输出在dataframe 中的一个变量中,或者替换原始变量,或者作为一个新变量。
我试图创建一个这样的函数,但我无法让它工作。
convert <- function(x) {
if(grep("M$", x)) {
str_sub(x, 1, -1)
as.numeric(x)
} else if(grep("k$", x)) {
str_sub(x, 1, -1)
as.numeric(x)
(x*0.001)
}
}
【问题讨论】:
标签: r dataframe data-manipulation