无法在 R 中分配多个行变量

Can't assign multiple line variables in R

我目前正在尝试学习 R,我遇到了一个可能很愚蠢的问题,但我找不到解决方案

我使用 RStudio,当我尝试分配一个变量时,我可以分配一行。 例如,如果我尝试 运行(我 select 所有行并单击 "Run" 按钮)此代码

age_survived <- summarise(group_by(train, Age, Survived), count=n())
    age_survived[which(age_survived$Survived==1), ] 
    rename(age_survived, "n_survived"="count")

它运行将行分开并将变量"age_survived"分配给第一行

所以我试着写这样的代码

 age_survived <- {
    summarise(group_by(train, Age, Survived), count=n())
    age_survived[which(age_survived$Survived==1), ] 
    rename(age_survived, "n_survived"="count")
 }

但是这样我得到这个错误

Error: object 'age_survived' not found

代码的唯一工作方式是这样

age_survived <- summarise(group_by(train, Age, Survived), count=n())
age_survived <- age_survived[which(age_survived$Survived==1), ] 
age_survived <- rename(age_survived, "n_survived"="count")

我认为这不是方法,我做错了什么?

Pipes 可能就是您要找的。 此代码应该适用于您应该使用的 train.csv 数据。

age_survived <- summarise(group_by(train, Age, Survived), count=n()) %>%
  filter(Survived == 1) %>%
  rename("n_survived" = "count")