繁体   English   中英

按字符拆分行元素并转换为向量

[英]Split row elements by character and transform into a vector

我有一个数据框看起来像:

library(tidyverse)

df <- tibble::tribble(
        ~respondent,           ~selection,
                  1, "Brain/Energy/Sleep",
                  2,    "Energy/Mood/Sex",
                  3,   "Detox/Sex/Stress"
        )

我想计算每行中的唯一元素,然后将它们拆分为每个“/”,因此将列selection转换为:

selection <- c("selection", "Brain", "Energy", "Sleep", "Energy", "Mood", "Sex", "Detox", "Sex", "Stress")

如何使用 dplyr 执行此操作?

new_vec <- 
  df %>% 
  pull(selection) %>% 
  strsplit("/") %>% 
  unlist() %>% 
  c("selection", .)

new_vec
 [1] "selection" "Brain"     "Energy"    "Sleep"     "Energy"    "Mood"      "Sex"      
 [8] "Detox"     "Sex"       "Stress"   

这是另一种消除每行中重复元素的解决方案。 我在 df 中添加了一个重复元素来说明这一点。

#Your data with a duplicate element
df <- tibble::tribble(
  ~respondent,           ~selection,
  1, "Brain/Energy/Sleep/Sleep",
  2,    "Energy/Mood/Sex/Energy",
  3,   "Detox/Sex/Stress/Detox"
)

#Number of columns expected after splitting each row on "/"
ncols_exp<-4

#Getting the distinct values per row (respondent)
df %>%
#Separate each entry in selection as multiple columns
  separate(col = selection,
           into = paste0("Var", 1:ncols_exp),
           sep = "/") %>%
  #Transform data into long format
  pivot_longer(cols = starts_with("Var"),
               names_to = "Var",
               values_to = "Val") %>%
  #Group by "respondent" (each row in the original df)
  group_by(respondent) %>%
  #Get unique elements of the Val column 
  distinct(Val) %>%
  #Pull Val column
  pull(Val) %>%
  #Concatenate the unique values with "selection" as the first entry
  c("selection", .)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM