在我的向量上有不同的数据,我一直在试图转换它,但是我真的找不到方法。
在te列中,我有重量,没有指标的是磅,其他的在KG,我需要所有的都用磅。但是,我没有找到如何只处理特定数目的行。拿出公斤,然后乘以2.20,让它以磅为单位。
发布于 2018-11-19 09:47:24
试试这个例子:
# example data
df1 <- read.table(text = "Weight
1 194
2 200
3 250
4 50Kg
5 40Kg
6 39Kg", header = TRUE, stringsAsFactors = FALSE)
# using ifelse (gives warning)
ifelse(grepl("Kg", df1$Weight),
as.numeric(gsub("Kg", "", df1$Weight)) * 2.2,
as.numeric(df1$Weight))
# [1] 194.0 200.0 250.0 110.0 88.0 85.8
# Warning message:
# In ifelse(grepl("Kg", df1$Weight), as.numeric(gsub("Kg", "", df1$Weight)) * :
# NAs introduced by coercion
# not using ifelse :)
as.numeric(gsub("Kg", "", df1$Weight)) * (1 + grepl("Kg", df1$Weight) * 1.2)
# [1] 194.0 200.0 250.0 110.0 88.0 85.8https://stackoverflow.com/questions/53371713
复制相似问题