R语言 如何修复“无法找到函数“%>%〈-”

bihw5rsg  于 2023-02-14  发布在  其他
关注(0)|答案(1)|浏览(421)

我运行下面的脚本之前没有任何问题。但我现在得到这个错误消息:动物_df %〉% hamster_dt = Dataframe 中的错误(“仓鼠”,2.879301):找不到函数“%〉%〈-”
pet.csv文件的内容如下:
| 宠物|长度|
| - ------|- ------|
| 猫|30.869472美元|
| 制动爪|50.729420美元|
| 鸟|六、一一三五零二|
| 鼠标|二、七五九二二二|
我使用了下面的代码。我曾经能够将物种“hamster”添加到表中,用四舍五入的长度值创建一个新列“size”,并计算每个大小的物种数量。(抱歉,这个愚蠢的例子...)

#load package
library(dplyr)

#set work directory
setwd('C:/Users/...')

#import csv file
animal_df <- read.csv('pets.csv', header=TRUE, sep = ',')

#count number of species per size
animal_df %>%
  hamster_dt <- data.frame('hamster', 2.879301)
  names(hamster_dt) <- c('pet', 'length')
  all_pets <- bind_rows(animal_df, hamster_dt)%>%
    mutate(size = ceiling(length)) %>%
    str(all_pets)
    sp_per_size <- table(all_pets$size)
    
View(sp_per_size)
write.csv(sp_per_size, "C:/Users/.../sp_per_size.csv", row.names=TRUE)
bvhaajcl

bvhaajcl1#

您将%>%散布在两个错误的位置。我们将注解掉animal_df %>%,然后删除mutatestr之间的%>%

# since I don't have your data
# animal_df <- read.csv('pets.csv', header=TRUE, sep = ',')
animal_df <- data.frame(pet="cat", length=14)

#count number of species per size
# animal_df %>%
hamster_dt <- data.frame('hamster', 2.879301)
names(hamster_dt) <- c('pet', 'length')
all_pets <- bind_rows(animal_df, hamster_dt)%>%
  mutate(size = ceiling(length))
str(all_pets)
# 'data.frame': 2 obs. of  3 variables:
#  $ pet   : chr  "cat" "hamster"
#  $ length: num  14 2.88
#  $ size  : num  14 3
sp_per_size <- table(all_pets$size)

相关问题