英文:
Replace NA value with a bunch of randomly generated values in R
问题
我想筛选我的“Title”列,然后用一些在最小和最大年龄范围内随机生成的值替换“Age”列中的NA值。我尝试了以下命令
titanic_df4 <- titanic_df3 %>%
filter(titanic_df3$Title=='Mr.') %>%
mutate(Age = replace_na(Age, runif(1, min=14, max=57)))
但它只用范围内的一个特定值替换了NA值。我应该怎么办?
尝试这个
titanic_df4 <- titanic_df3 %>%
filter(titanic_df3$Title=='Mr.') %>%
mutate(Age = replace_na(Age, runif(1, min=14, max=57)))
英文:
I want to filter my Title column and then replace the NA values in Age column with a bunch of randomly generated values(within min and max age range). I tried the following command
titanic_df4 <- titanic_df3 %>% filter(titanic_df3$Title=='Mr.') %>% mutate(Age = replace_na(Age, runif(1, min=14, max=57)))
but it just replace the na value with one specific value within the range. What should I do?
Tried this
titanic_df4 <- titanic_df3 %>% filter(titanic_df3$Title=='Mr.') %>% mutate(Age = replace_na(Age, runif(1, min=14, max=57)))
答案1
得分: 1
replace_na 只适用于单个值。从 ?replace_na 帮助页面:
> 如果 data 是一个向量,replace 接受一个单个值。此单个值替代向量中的所有 NA 值。
相反,我们可以生成足够的值用于整个向量,并使用 coalesce() 填充 NA 值,使用这些生成的值。
titanic_df3 %>%
## Title not titanic_df3$Title
filter(Title == 'Mr.') %>%
mutate(Age = coalesce(Age, runif(n(), min=14, max=57)))
英文:
replace_na only works for single values. From the ?replace_na help page:
> If data is a vector, replace takes a single value. This single value replaces all of the NA values in the vector.
Instead we can generate enough values for the whole vector and use coalesce() to fill in the NA values with those generated values.
titanic_df3 %>%
## Title not titanic_df3$Title
filter(Title == 'Mr.') %>%
mutate(Age = coalesce(Age, runif(n(), min=14, max=57)))
</details>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论