R 跨多个变量粘贴变量名及其向量值

R 跨多个变量粘贴变量名及其向量值,r,dplyr,rlang,tidyeval,R,Dplyr,Rlang,Tidyeval,假设我有这个数据帧: library(dplyr) set.seed(1) df <- tibble(sol_1 = sample(1:4, 10, replace = TRUE), sol_2 = sample(1:4, 10, replace = TRUE)) # A tibble: 10 x 2 sol_1 sol_2 <int> <int> 1 1 3 2 4 3 3 3

假设我有这个数据帧:

library(dplyr)

set.seed(1)
df <- tibble(sol_1 = sample(1:4, 10, replace = TRUE),
             sol_2 = sample(1:4, 10, replace = TRUE))

# A tibble: 10 x 2
   sol_1 sol_2
   <int> <int>
 1     1     3
 2     4     3
 3     3     1
 4     1     1
 5     2     1
 6     1     2
 7     3     2
 8     3     2
 9     2     2
10     2     3

但显然我使用了错误的函数。

使用
mutate.*
很难完成此任务,因为它只传递列的值而没有名称。相反,我们可以尝试
map\u dfc
apply

library(dplyr)
library(purrr)
nms = grep("sol", names(df), value = TRUE)
map_dfc(nms, ~transmute(df, !!quo_name(.x) := paste0(.x,'_',!!sym(.x))))
使用基数R

df[ ,nms] = t(apply(df[,nms], 1, function(x) paste0(names(x),'_',x)))

使用mutate可以实现同样的效果:

library(dplyr)
    df %>% mutate(sol_1 = paste0(names(df)[1], '_', sol_1),
                  sol_2 = paste0(names(df)[2], '_', sol_2))

另一种选择是将其重塑为“长”,然后在转换后将其改回“宽”

library(dplyr)
library(tidyr)
library(stringr)
df %>%
   mutate(rn = row_number()) %>% 
   pivot_longer(cols = -rn) %>% 
   mutate(value = str_c(name, value, sep="_")) %>% 
   pivot_wider(names_from = name, values_from = value) %>%
   select(-rn)
# A tibble: 10 x 2
#   sol_1   sol_2  
#   <chr>   <chr>  
# 1 sol_1_1 sol_2_3
# 2 sol_1_4 sol_2_3
# 3 sol_1_3 sol_2_1
# 4 sol_1_1 sol_2_1
# 5 sol_1_2 sol_2_1
# 6 sol_1_1 sol_2_2
# 7 sol_1_3 sol_2_2
# 8 sol_1_3 sol_2_2
# 9 sol_1_2 sol_2_2
#10 sol_1_2 sol_2_3
基本R解决方案:

df[] <- lapply(seq_along(df),

               function(x){df[,x] <- paste0(names(df[c(x)]), "_", unlist(df[c(x)]))})
df[]
library(dplyr)
library(tidyr)
library(stringr)
df %>%
   mutate(rn = row_number()) %>% 
   pivot_longer(cols = -rn) %>% 
   mutate(value = str_c(name, value, sep="_")) %>% 
   pivot_wider(names_from = name, values_from = value) %>%
   select(-rn)
# A tibble: 10 x 2
#   sol_1   sol_2  
#   <chr>   <chr>  
# 1 sol_1_1 sol_2_3
# 2 sol_1_4 sol_2_3
# 3 sol_1_3 sol_2_1
# 4 sol_1_1 sol_2_1
# 5 sol_1_2 sol_2_1
# 6 sol_1_1 sol_2_2
# 7 sol_1_3 sol_2_2
# 8 sol_1_3 sol_2_2
# 9 sol_1_2 sol_2_2
#10 sol_1_2 sol_2_3
library(purrr)
imap(df, ~ str_c(.y, .x, sep="_"))
df[] <- lapply(seq_along(df),

               function(x){df[,x] <- paste0(names(df[c(x)]), "_", unlist(df[c(x)]))})
df <- tibble(sol_1 = sample(1:4, 10, replace = TRUE),

             sol_2 = sample(1:4, 10, replace = TRUE))