I'm given a charachter vector of heights looking like that:
[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
How can I turn it into a numeric vector of cm ?
thank you
Use -
# 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)
Output
> 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