gpt4 book ai didi

r - "Error in UseMethod("mutate ") : no applicable method for ' mutate ' applied to an object of class "function"尝试分隔列时

转载 作者:行者123 更新时间:2023-12-04 07:34:18 28 4
gpt4 key购买 nike

所以我有这个数据集

# 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。您没有为您的数据框命名 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>

关于r - "Error in UseMethod("mutate ") : no applicable method for ' mutate ' applied to an object of class "function"尝试分隔列时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67812413/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com