【问题标题】:Can't assign multiple line variables in R无法在 R 中分配多个行变量
【发布时间】:2020-08-09 06:27:40
【问题描述】:

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

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

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")
 }

但是这样我得到了这个错误

错误:找不到对象“age_survived”

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

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")

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

【问题讨论】:

  • 其实就是这样。好吧,无论如何都是一种方式。

标签: r variables variable-assignment


【解决方案1】:

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

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

【讨论】:

  • 非常感谢,它有效!我已经知道管道,但我并不真正了解它们是如何工作的,现在更清楚了
猜你喜欢
  • 2014-10-08
  • 1970-01-01
  • 1970-01-01
  • 2019-04-04
  • 2014-10-09
  • 2011-09-25
  • 1970-01-01
  • 1970-01-01
  • 2014-06-04
相关资源
最近更新 更多