1

在此处输入图像描述

这是我第一次使用管道功能,我的教授还没有审查如何使用它,所以

我有点迷茫,我在最后一个问题上遇到了麻烦,因为自上次以来我很可能一直出错

分配与我的过滤器<=2相矛盾,提前谢谢你,以下是我的代码:

L.W<- iris %>%
select(Petal.Length,Petal.Width) %>% head()
print(L.W)  

#b
S.L <- iris%>%
arrange(Sepal.Length)%>%
head()
print(S.L)  

#c
iris%>
arrange(Sepal.Length)%>%
select(Species,Petal.Length,Petal.Width)%>%
head()

#Switch order 
iris%>%
select(Species,Petal.Length,Petal.Width)%>%
head()

#there are two different data sets 

#d
iris%>%
filter(Petal.Length<=2 & Petal.Width< mean(Petal.Width))%>%
mutate(Petal.Length)%>%
huge<-assign(Petal.Length>6)%>%
big<-assign(Petal.Length>5)%>%
medium<-assign(Petal.Length>4)%>%
small<-assign(Petal.Length<=4)%>%
head()
4

1 回答 1

1

说明:任务 #d 的解决方案:使用filterPetal.Length 不 <=2 的选择观察!Petal.Length <= 2并且......然后我们使用mutatewithcase_when

#d
iris %>% 
  filter(!Petal.Length <= 2 & !Petal.Width < mean(Petal.Width)) %>% 
  mutate(new_col = case_when(Petal.Length > 6 ~ "huge",
                             Petal.Length > 5 ~ "big",
                             Petal.Length > 4 ~ "medium",
                             Petal.Length <= 4 ~ "small")) %>% 
  head()
  

输出:

  Sepal.Length Sepal.Width Petal.Length Petal.Width    Species new_col
1          7.0         3.2          4.7         1.4 versicolor  medium
2          6.4         3.2          4.5         1.5 versicolor  medium
3          6.9         3.1          4.9         1.5 versicolor  medium
4          5.5         2.3          4.0         1.3 versicolor   small
5          6.5         2.8          4.6         1.5 versicolor  medium
6          5.7         2.8          4.5         1.3 versicolor  medium
于 2021-03-28T19:31:36.367 回答