[英]R recode a column based on a string value
I have a dataframe as follows 我有一个数据框如下
date volume
1-1-90 1.1M
2-1-90 200
3-1-90 0.5M
4-1-90 100
5-1-90 1M
The values with M means in millions. M的值表示百万。 I would like to detect the values with letter M or m in them and transform these values into the numerical equivalents
我想检测其中带有字母M或m的值,并将这些值转换为数值等效项
date volume
1-1-90 1100000
2-1-90 200
3-1-90 500000
4-1-90 100
5-1-90 10000000
Is there a nifty way of doing it in R? 在R中有一种不错的方法吗?
I have used an ifelse condition as follows 我使用了ifelse条件,如下所示
(df)[, Volumes := ifelse(volume %in% c("m", "M"),volume * 1000000,0)]
but this does not seem to work. 但这似乎不起作用。 Am sure am overlooking which must be trivial.
我肯定忽略了那一定是微不足道的。
> dat$volume <- ifelse( grepl("M|m" ,dat$volume),
1e6*as.numeric(sub("M|m","", dat$volume)),
as.numeric(as.character(dat$volume) ) )
> dat
date volume
1 1-1-90 1100000
2 2-1-90 200
3 3-1-90 500000
4 4-1-90 100
5 5-1-90 1000000
It seems to me like you have a data.table
object there (or maybe you mistakenly using data.table
syntax on a data.frame
?) 在我看来,就像你有一个
data.table
对象有(或者也许你错误地使用data.table
在语法data.frame
?)
Anyway, if df
is a data.table
object, I would go with 无论如何,如果
df
是一个data.table
对象,我会选择
df[grepl("m", volume, ignore.case = T),
volume2 := as.numeric(gsub("m", "", volume, ignore.case = T)) * 1e6]
df[is.na(volume2), volume2 := as.numeric(as.character(volume))][, volume := NULL]
df
# date volume2
# 1: 1-1-90 1100000
# 2: 2-1-90 200
# 3: 3-1-90 500000
# 4: 4-1-90 100
# 5: 5-1-90 1000000
The stringr
package can also work here: stringr
包也可以在这里工作:
require(stringr)
dat$volume <- ifelse(str_sub(dat$volume, -1) == "M"
,as.numeric(str_sub(dat$volume, 0, nchar(dat$volume)-1))*1000000
,dat$volume)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.