gpt4 book ai didi

r - 如何将一个字符串列分隔成多个列?

转载 作者:行者123 更新时间:2023-12-04 00:49:23 25 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

我想将这个字符串列分成多个列,以便在其自己的列中获取每个社交媒体。

最佳答案

您可以使用 tidytext 包中的 unnest_tokens 结合 tidyr 中的 spread 来获得您想要的效果重新...

library(tidyverse)
library(tidytext)

df %>%
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")

(我生成了我自己的数据版本,因此这看起来与您的不同)

# 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

关于r - 如何将一个字符串列分隔成多个列?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67806994/

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