简体   繁体   English

R ifelse 在 mutate 中引用第二个 data.frame

[英]R ifelse in mutate referencing a second data.frame

I am experimenting with building a simple model predicting sex from height.我正在尝试构建一个简单的 model 从身高预测性别。 I know that the following is quite naive, but I am stuck with a R syntax problem.. First I create a dataframe with a probability for female for each height group.我知道以下内容很天真,但我遇到了 R 语法问题。首先,我创建了一个 dataframe,每个身高组的女性概率为 dataframe。 Then I want to look up this model to assign sex for the test data according to a certain limit然后我要查这个model给测试数据按照一定的限度分配性别

library(tidyverse)
library(dslabs)
library(caret)
# rm(list=ls())
ind <- createDataPartition(heights$height,1, p=0.5, list = FALSE) 
test_height <- heights %>% slice(ind)
train_height <- heights %>% slice(-ind)
head(train_height)

model <- train_height %>% mutate(height=round(height)) %>% group_by(height) %>% summarise(pf = mean(sex=="Female"))

y_hat <- test_height %>% mutate(height=round(height))  %>% mutate(pred = ifelse(model[model$height == height, ]$pf > 0.5, "Female", "Male"))

I get我明白了

Error in `mutate()`:
! Problem while computing `pred = ifelse(model[model$height == height, ]$pf > 0.5,
  "Female", "Male")`.
Caused by error:
! Must subset rows with a valid subscript vector.
i Logical subscripts must match the size of the indexed input.
x Input has size 28 but subscript `model$height == height` has size 526.

How can I lookup the right model row in my ifelse expression?如何在我的 ifelse 表达式中查找正确的 model 行?

Thanks,谢谢,

Hans汉斯

I would left_join(test_height, model, by=height) to match the model$pf values to test_height according to the right height, and then mutate on ifelse(pf > 0.5,...) .我会left_join(test_height, model, by=height)根据正确的高度将 model$pf 值与 test_height 匹配,然后在ifelse(pf > 0.5,...)上进行mutate It seems simpler to me.这对我来说似乎更简单。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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