将一个变量的多个副本连接到一个变量中,并在R中使用一个大数据集

将一个变量的多个副本连接到一个变量中,并在R中使用一个大数据集,r,loops,variables,concatenation,R,Loops,Variables,Concatenation,我有一个数据集,每个变量有多个版本。所有变量都以_1、_2、_3结尾。我想将变量的各种版本连接到一个新列中。我有一个非常大的数据集,因此我希望尽量避免手动编码每个变量(例如dat$test我们可以使用melt从数据。表和将元素粘贴在一起 library(data.table) melt(setDT(df1, keep.rownames = TRUE), measure = patterns('^test', 'type', 'other'), value.name = c(

我有一个数据集,每个变量有多个版本。所有变量都以_1、_2、_3结尾。我想将变量的各种版本连接到一个新列中。我有一个非常大的数据集,因此我希望尽量避免手动编码每个变量(例如dat$test我们可以使用
melt
数据。表
将元素粘贴在一起

library(data.table)
melt(setDT(df1, keep.rownames = TRUE), measure  =
      patterns('^test', 'type', 'other'),
    value.name = c('test', 'type', 'other'))[, 
       variable := NULL][, lapply(.SD, paste, collapse=""),
      .(rn)][, rn := NULL][]
#    test type other
#1:  aff  dst   jyb
#2:  sdc  vsy   ham
#3:  dsv  dhu   njk

或者在tidyverse中使用类似的方法

library(dplyr)
library(tidyr)
library(stringr)
df1 %>%
   mutate(rn = row_number()) %>%
   pivot_longer(cols = -rn,  names_to = c( ".value", 'grp'), names_sep="_") %>% 
   group_by(rn) %>%
   summarise_at(vars(test:other), str_c, collapse ="") %>%
   select(-rn)
# A tibble: 3 x 3
#  test  type  other
#  <chr> <chr> <chr>
#1 aff   dst   jyb  
#2 sdc   vsy   ham  
#3 dsv   dhu   njk  
库(dplyr)
图书馆(tidyr)
图书馆(stringr)
df1%>%
变异(rn=行数())%>%
pivot_longer(cols=-rn,names_to=c(“.value”,“grp”),names_sep=“211;”)%>%
分组依据(rn)%>%
总结(变量(测试:其他)、str_c、collapse=“”)%>%
选择(-rn)
#一个tibble:3x3
#测试类型其他
#    
#1 aff dst jyb
#2 sdc vsy火腿
#3 dsv dhu njk
数据
df1
library(data.table)
melt(setDT(df1, keep.rownames = TRUE), measure  =
      patterns('^test', 'type', 'other'),
    value.name = c('test', 'type', 'other'))[, 
       variable := NULL][, lapply(.SD, paste, collapse=""),
      .(rn)][, rn := NULL][]
#    test type other
#1:  aff  dst   jyb
#2:  sdc  vsy   ham
#3:  dsv  dhu   njk
library(dplyr)
library(tidyr)
library(stringr)
df1 %>%
   mutate(rn = row_number()) %>%
   pivot_longer(cols = -rn,  names_to = c( ".value", 'grp'), names_sep="_") %>% 
   group_by(rn) %>%
   summarise_at(vars(test:other), str_c, collapse ="") %>%
   select(-rn)
# A tibble: 3 x 3
#  test  type  other
#  <chr> <chr> <chr>
#1 aff   dst   jyb  
#2 sdc   vsy   ham  
#3 dsv   dhu   njk  
df1 <- structure(list(test_1 = c("a", "s", "d"), test_2 = c("f", "d", 
"s"), test_3 = c("f", "c", "v"), type_1 = c("d", "v", "d"), type_2 = c("s", 
"s", "h"), type_3 = c("t", "y", "u"), other_1 = c("j", "h", "n"
), other_2 = c("y", "a", "j"), other_3 = c("b", "m", "k")),
   class = "data.frame", row.names = c(NA, 
-3L))