简体   繁体   English

“找不到函数%>%<-”,tidyr软件包和%>%运算符出现问题

[英]“could not find function %>%<-”, issue with tidyr package and the %>% operator

I'm working on a script for a swirl lesson on using the tidyr package and I'm having some trouble with the %>% operator. 我正在编写有关使用tidyr软件包的旋涡课程的脚本,但是%>%运算符遇到了一些麻烦。 I've got a data frame called passed that contains the name, class number, and final grade of 4 students. 我有一个名为pass的数据框,其中包含4名学生的姓名,班级编号和最终成绩。 I want to add a new column called status and populate it with a character vector that says "passed". 我想添加一个名为status的新列,并用一个表示“已通过”的字符向量进行填充。 Before that, I used select to grab some columns from a data frame called students4 and stored it in a data frame called grade book 在此之前,我使用select从一个名为“ students4”的数据框中获取一些列,并将其存储在一个名为“成绩簿”的数据中。

gradebook <- students4 %>%
select(id, class, midterm, final) %>%
passed<-passed %>% mutate(status="passed")

Swirl problems build on each other, and the last one just had me running the first to lines of code, so I think those two are correct. 漩涡问题是相互依存的,最后一个只是让我在代码行中运行第一个,所以我认为这两个是正确的。 The third line was what was suggested after a couple of wrong attempts, so I think there's something about %>% that I'm not understanding. 第三行是经过几次错误尝试后提出的建议,所以我认为有些%>%的内容我不理解。 When I run the code I get an error that says; 运行代码时,我看到一条错误消息:

Error in students4 %>% select(id, class, midterm, final) %>% passed <- passed %>%  : 
  could not find function "%>%<-

I found another user who asked about the "could not find function "%>%" who was able to resolve the issue by installing the magrittr package, but that didn't do the trick for me. Any input on the issues in my code would be super appreciated! 我发现另一个询问“找不到函数"%>%"用户能够通过安装magrittr软件包解决问题,但这对我没有帮助。在我的代码中对问题的任何输入会非常感激!

It's not a problem with the package or the operator. 包装或操作员都没有问题。 You're trying to pipe into a new line with a new variable. 您正在尝试使用新变量插入新行。

The %>% passes the previous dataframe into the next function as that functions df argument. %>%将前一个数据帧传递给下一个函数,即该函数的df参数。

Instead of doing all of this: 而不是做所有这些:

Gradebook <- select(students4, id, class, midterm, final)
Gradebook2 <- mutate(Gradebook, test4 = 100) 
Gradebook3 <- arrange(Gradebook2, desc(final))

You can pipe operator into the next argument if you're working on the same dataframe. 如果您在同一数据框上工作,则可以将运算符传递到下一个参数中。

Gradebook <- students4 %>% 
      select(students4, id, class, midterm, final) %>%
      mutate(test4 = 100) %>%
      arrange(desc(final))

Much cleaner and easier to read. 更清洁,更易于阅读。

In your second line you're trying to pass it to a new function but instead of there being a function you're all of a sudden defining a variable. 在第二行中,您尝试将其传递给新函数,但是突然有了一个变量,而不是一个函数。 I don't know the exercise you're doing but you should remove the second operator. 我不知道您正在执行的练习,但是您应该删除第二个运算符。

gradebook <- students4 %>%
select(id, class, midterm, final)

passed <- passed %>% mutate(status="passed")

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

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