简体   繁体   中英

Error: 'list' object cannot be coerced to type 'double' in R

I'm new to R. I'm trying to get the SD of weight in lbs. First I'm getting the weight in lbs from a dataset with weight in kg. When I get type of() for the result, it's a list. But in the console, its a 'list' of 'dbl'. I've tried 'as.numeric()' and 'as.integer()' in the pipe but both give the same error. How can I get the SD?

I have other questions that have similar issues (data type being a list when they should be numeric) so if you can explain why that's happening that would be great!

weight_lbs <- brfss %>%
  clean_names(., "lower_camel") %>%
  select(havarth3, wtkg3)%>%  
  filter(havarth3 == "1")%>%
  na.omit()%>%
  mutate(weight_lbs=(round(wtkg3*2.20462)/100),2)%>%
  select(weight_lbs)%>%
  as.numeric()
weight_lbs

sd_weight <- sd(weight_lbs, na.rm=TRUE)

Try this code: I think as.numeric() alone won't work. wrap it into a mutate:

weight_lbs <- brfss %>%
  clean_names(., "lower_camel") %>%
  select(havarth3, wtkg3)%>%  
  filter(havarth3 == "1")%>%
  na.omit()%>%
  mutate(weight_lbs=(round(wtkg3*2.20462)/100),2)%>%
  select(weight_lbs)%>%
  mutate(weight_lbs = as.numeric(weight_lbs)) %>% 
  mutate(sd_weight_lbs = sd(weight_lbs))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM