"Error in UseMethod("mutate") : 'mutate' 的适用方法在尝试分隔列时应用于 class "function" 的对象
"Error in UseMethod("mutate") : no applicable method for 'mutate' applied to an object of class "function" when trying to seperate columns
所以我有这个数据集
# A tibble: 268 x 1
`Which of these social media platforms do you have an account in right now?`
<chr>
1 Facebook, Instagram, Twitter, Snapchat, Reddit, Signal
2 Reddit
3 Facebook, Instagram, Twitter, Linkedin, Snapchat, Reddit, Quora
4 Facebook, Instagram, Twitter, Snapchat
5 Facebook, Instagram, TikTok, Snapchat
6 Facebook, Instagram, Twitter, Linkedin, Snapchat
7 Facebook, Instagram, TikTok, Linkedin, Snapchat, Reddit
8 Facebook, Instagram, Snapchat
9 Linkedin, Reddit
10 Facebook, Instagram, Twitter, TikTok
# ... with 258 more rows
我想把它分成多列,每个变量都用是和否,就像这样
# A tibble: 268 x 8
Id Facebook Instagram Reddit Signal Snapchat TikTok Twitter
<int> <chr> <chr> <chr> <chr> <chr> <chr> <chr>
1 1 No No No No No No Yes
2 2 Yes Yes No No Yes No Yes
3 3 No Yes No Yes No Yes No
4 4 No Yes No No Yes No No
5 5 No Yes No Yes Yes Yes Yes
6 6 No Yes No No No No No
7 7 No No Yes Yes No Yes Yes
8 8 No No Yes No No No Yes
9 9 No No Yes No Yes Yes No
10 10 No Yes Yes Yes Yes No Yes
所以我写了这段代码来做到这一点
library(tidyverse)
library(tidytext)
Survey %>%
mutate(Id = row_number(), HasAccount = "Yes") %>%
unnest_tokens(Network, `Which of these social media platforms do you have an account in right now?`, to_lower = F) %>%
spread(Network, HasAccount, fill = "No")
但是我收到这个错误
Erreur : Must extract column with a single valid subscript.
x Subscript `var` has size 268 but must be size 1.
> dput(head(Survey[1:5]))
structure(list(Horodateur = structure(c(1619171956.596, 1619172695.039,
1619173104.83, 1619174548.534, 1619174557.538, 1619174735.457
), tzone = "UTC", class = c("POSIXct", "POSIXt")), `To_which_gender_you_identify_the_most?` = c("Male",
"Female", "Male", "Female", "Female", "Female"), What_is_your_age_group = c("[18-24[",
"[10,18[", "[18-24[", "[18-24[", "[18-24[", "[25,34["), How_much_time_do_you_spend_on_social_media = c("1-5 hours",
"1-5 hours", ">10 hours", "5-10 hours", "5-10 hours", "1-5 hours"
), `Which_of_these_social_media_platforms_do_you_have_an_account_in_right_now?` = c("Facebook, Instagram, Twitter, Snapchat, Reddit, Signal",
"Reddit", "Facebook, Instagram, Twitter, Linkedin, Snapchat, Reddit, Quora",
"Facebook, Instagram, Twitter, Snapchat", "Facebook, Instagram, TikTok, Snapchat",
"Facebook, Instagram, Twitter, Linkedin, Snapchat")), row.names = c(NA,
-6L), class = c("tbl_df", "tbl", "data.frame"))
编辑:根据@CSJCampbell 的回答编辑了问题。
编辑:添加了我正在使用的数据集的片段。
mutate
的第一个参数必须是 data.frame。您没有将 data.frame 命名为 df
,因此函数 df
被传递给 mutate
。
args(df)
# function (x, df1, df2, ncp, log = FALSE)
# NULL
编辑:更新后,您添加了 dput
数据输出。 运行 你的代码给我错误:
Survey %>%
mutate(Id = row_number(), HasAccount = "Yes") %>%
unnest_tokens(Network, `Which of these social media platforms do you have an account in right now?`, to_lower = F)
# Error in check_input(x) :
# Input must be a character vector of any length or a list of character
# vectors, each of which has a length of 1.
您的 dput
有以下划线命名的列:
colnames(Survey)[5]
# "Which_of_these_social_media_platforms_do_you_have_an_account_in_right_now?"
重命名列:
Survey %>%
transmute(Id = row_number(), HasAccount = "Yes",
Platforms = `Which_of_these_social_media_platforms_do_you_have_an_account_in_right_now?`) %>%
unnest_tokens(Network, Platforms) %>%
spread(Network, HasAccount, fill = "No")
# # A tibble: 6 x 10
# Id facebook instagram linkedin quora reddit
# <int> <chr> <chr> <chr> <chr> <chr>
# 1 1 Yes Yes No No Yes
# 2 2 No No No No Yes
# 3 3 Yes Yes Yes Yes Yes
# 4 4 Yes Yes No No No
# 5 5 Yes Yes No No No
# 6 6 Yes Yes Yes No No
# # … with 4 more variables: signal <chr>,
# # snapchat <chr>, tiktok <chr>, twitter <chr>
(不是这个确切问题的答案,但与我认为相关的 post 标题足够相似...)
我 运行 由于稍微不同(但相关)的原因而陷入非常相似的错误,即我混淆了 ggplot
和管道 (%>%
) 语法。我写道:
df %>% # <- pipe
summarize(...) + # <- NOT a pipe!
mutate(...)
导致错误:
Error in UseMethod("mutate") : no applicable method for 'mutate' applied to an object of class "name"
这是因为 mutate()
没有将数据框作为其第一个参数。我将其添加为答案而不是评论以强调这种错误,这种错误在 tidyverse
中工作时很容易犯,尤其是在操作数据和更新绘图之间切换时,但可能不是乍一看很明显。
所以我有这个数据集
# A tibble: 268 x 1
`Which of these social media platforms do you have an account in right now?`
<chr>
1 Facebook, Instagram, Twitter, Snapchat, Reddit, Signal
2 Reddit
3 Facebook, Instagram, Twitter, Linkedin, Snapchat, Reddit, Quora
4 Facebook, Instagram, Twitter, Snapchat
5 Facebook, Instagram, TikTok, Snapchat
6 Facebook, Instagram, Twitter, Linkedin, Snapchat
7 Facebook, Instagram, TikTok, Linkedin, Snapchat, Reddit
8 Facebook, Instagram, Snapchat
9 Linkedin, Reddit
10 Facebook, Instagram, Twitter, TikTok
# ... with 258 more rows
我想把它分成多列,每个变量都用是和否,就像这样
# A tibble: 268 x 8
Id Facebook Instagram Reddit Signal Snapchat TikTok Twitter
<int> <chr> <chr> <chr> <chr> <chr> <chr> <chr>
1 1 No No No No No No Yes
2 2 Yes Yes No No Yes No Yes
3 3 No Yes No Yes No Yes No
4 4 No Yes No No Yes No No
5 5 No Yes No Yes Yes Yes Yes
6 6 No Yes No No No No No
7 7 No No Yes Yes No Yes Yes
8 8 No No Yes No No No Yes
9 9 No No Yes No Yes Yes No
10 10 No Yes Yes Yes Yes No Yes
所以我写了这段代码来做到这一点
library(tidyverse)
library(tidytext)
Survey %>%
mutate(Id = row_number(), HasAccount = "Yes") %>%
unnest_tokens(Network, `Which of these social media platforms do you have an account in right now?`, to_lower = F) %>%
spread(Network, HasAccount, fill = "No")
但是我收到这个错误
Erreur : Must extract column with a single valid subscript.
x Subscript `var` has size 268 but must be size 1.
> dput(head(Survey[1:5]))
structure(list(Horodateur = structure(c(1619171956.596, 1619172695.039,
1619173104.83, 1619174548.534, 1619174557.538, 1619174735.457
), tzone = "UTC", class = c("POSIXct", "POSIXt")), `To_which_gender_you_identify_the_most?` = c("Male",
"Female", "Male", "Female", "Female", "Female"), What_is_your_age_group = c("[18-24[",
"[10,18[", "[18-24[", "[18-24[", "[18-24[", "[25,34["), How_much_time_do_you_spend_on_social_media = c("1-5 hours",
"1-5 hours", ">10 hours", "5-10 hours", "5-10 hours", "1-5 hours"
), `Which_of_these_social_media_platforms_do_you_have_an_account_in_right_now?` = c("Facebook, Instagram, Twitter, Snapchat, Reddit, Signal",
"Reddit", "Facebook, Instagram, Twitter, Linkedin, Snapchat, Reddit, Quora",
"Facebook, Instagram, Twitter, Snapchat", "Facebook, Instagram, TikTok, Snapchat",
"Facebook, Instagram, Twitter, Linkedin, Snapchat")), row.names = c(NA,
-6L), class = c("tbl_df", "tbl", "data.frame"))
编辑:根据@CSJCampbell 的回答编辑了问题。 编辑:添加了我正在使用的数据集的片段。
mutate
的第一个参数必须是 data.frame。您没有将 data.frame 命名为 df
,因此函数 df
被传递给 mutate
。
args(df)
# function (x, df1, df2, ncp, log = FALSE)
# NULL
编辑:更新后,您添加了 dput
数据输出。 运行 你的代码给我错误:
Survey %>%
mutate(Id = row_number(), HasAccount = "Yes") %>%
unnest_tokens(Network, `Which of these social media platforms do you have an account in right now?`, to_lower = F)
# Error in check_input(x) :
# Input must be a character vector of any length or a list of character
# vectors, each of which has a length of 1.
您的 dput
有以下划线命名的列:
colnames(Survey)[5]
# "Which_of_these_social_media_platforms_do_you_have_an_account_in_right_now?"
重命名列:
Survey %>%
transmute(Id = row_number(), HasAccount = "Yes",
Platforms = `Which_of_these_social_media_platforms_do_you_have_an_account_in_right_now?`) %>%
unnest_tokens(Network, Platforms) %>%
spread(Network, HasAccount, fill = "No")
# # A tibble: 6 x 10
# Id facebook instagram linkedin quora reddit
# <int> <chr> <chr> <chr> <chr> <chr>
# 1 1 Yes Yes No No Yes
# 2 2 No No No No Yes
# 3 3 Yes Yes Yes Yes Yes
# 4 4 Yes Yes No No No
# 5 5 Yes Yes No No No
# 6 6 Yes Yes Yes No No
# # … with 4 more variables: signal <chr>,
# # snapchat <chr>, tiktok <chr>, twitter <chr>
(不是这个确切问题的答案,但与我认为相关的 post 标题足够相似...)
我 运行 由于稍微不同(但相关)的原因而陷入非常相似的错误,即我混淆了 ggplot
和管道 (%>%
) 语法。我写道:
df %>% # <- pipe
summarize(...) + # <- NOT a pipe!
mutate(...)
导致错误:
Error in UseMethod("mutate") : no applicable method for 'mutate' applied to an object of class "name"
这是因为 mutate()
没有将数据框作为其第一个参数。我将其添加为答案而不是评论以强调这种错误,这种错误在 tidyverse
中工作时很容易犯,尤其是在操作数据和更新绘图之间切换时,但可能不是乍一看很明显。