Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
365 views
in Technique[技术] by (71.8m points)

transform - How to convert feet to cm in R?

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...