【问题标题】:How to convert feet to cm in R?如何在R中将英尺转换为厘米?
【发布时间】:2020-03-21 23:34:53
【问题描述】:
我得到了一个看起来像这样的高度字符向量:
[859] 5'10 5'8 5'11 6'0 5'10 6'2 5'11 6'2 6'2 5'7 5'9 5'7 6'1 6'0 5'11 6'0 6'5 6'1 6'1 5'10 5'11 5'11 6'0 6'4 6'5 5'10
[885] 6'0 6'1 5'10 5'10 5'8 6'1 6'0 5'11 6'0 5'8 5'8 6'1 5'11 6'2 5'8 5'11 6'0 6'3 5'8 6'0 6'2
如何将其转换为 cm 的数值向量?
谢谢
【问题讨论】:
标签:
r
transform
units-of-measurement
measure
【解决方案1】:
使用 -
# install.packages("measurements")
library("measurements")
ft_inch <- function(str_ft_inch){
elem <- as.integer(unlist(strsplit(str_ft_inch, "'")))
inch <- elem[1]*12 + elem[2]
return(conv_unit(inch, "inch", "cm"))
}
df["cm"] <- sapply(df[,"height"], ft_inch)
输出
> df[,"cm"]
[1] 177.80 172.72 180.34 182.88 177.80 187.96 180.34 187.96 187.96 170.18
[11] 175.26 170.18 185.42 182.88 180.34 182.88 195.58 185.42 185.42 177.80
[21] 180.34 180.34 182.88 193.04 195.58 177.80 182.88 185.42 177.80 177.80
[31] 172.72 185.42 182.88 180.34 182.88 172.72 172.72 185.42 180.34 187.96
[41] 172.72 180.34 182.88 190.50 172.72 182.88 187.96