简体   繁体   English

循环在 r 中的多个列中应用 dyplr

[英]Loop to apply dyplr across multiple columns in r

I have a dataset我有一个数据集

在此处输入图像描述

outcome_data_wide_score1 <- outcome_data %>% select(group, community, site, sessions, patientid, score1) %>% arrange(sessions) %>% filter(group == 2 & province == "X") %>% pivot_wider(., names_from =sessions, values_from = c(score1))

I am trying to run these codes get the similar output for different score columns and if possible few lines of codes which will run the codes for different group, community and site by some list or other command我正在尝试运行这些代码为不同的分数列获取类似的 output,如果可能的话,几行代码将通过一些列表或其他命令运行不同组、社区和站点的代码

Any help in this regards will be appreciable.在这方面的任何帮助都将是可观的。

Using dummy data similar to those posted in the question, you can create an index based on group , community and site .使用类似于问题中发布的虚拟数据,您可以创建基于groupcommunitysite的索引。 Split it into a list, apply the task for all scores and then bind all together.将其拆分为列表,将任务应用于所有分数,然后将所有内容绑定在一起。 I have added a function to do this:我添加了一个 function 来执行此操作:

library(reshape2)
library(tidyverse)
set.seed(123)
#Data
df <- data.frame(group=1,community=c(rep('x',6),rep('y',3)),
                 site=c(rep(c('a'),3),rep(c('b'),3),rep(c('c'),3)),
                 patientid=c(rep(1,3),rep(2,3),rep(3,3)),
                 sessions=rep(c(1,2,3),3),
                 score1=round(runif(9,0,100),0),
                 score2=round(runif(9,0,100),0),
                 score3=round(runif(9,0,100),0),
                 score4=round(runif(9,0,100),0),
                 score5=round(runif(9,0,100),0))
#Create an id by the columns you want
df$id <- paste(df$group,df$community,df$site)
#Now split
List <- split(df,df$id)
#Function to process
myfun <- function(x)
{
  #Filter columns
  y <- x[,names(x)[which(grepl(c('patient|session|score'),names(x)))]]
  #Melt
  z <- melt(y,id.vars = c('patientid','sessions'))
  #Transform
  u <- pivot_wider(z, names_from =c(variable,sessions), values_from = value)
  #Combine and separate
  ids <- x[,'id',drop=F]
  ids <- ids[!duplicated(ids$id),,drop=F]
  idsg <- separate(ids,col = id,sep = ' ',into = c('group','community','site'))
  #Bind
  w <- bind_cols(idsg,u)
  return(w)
}
#Now apply to List
List2 <- lapply(List,myfun)
#Bind all
DF <- do.call(rbind,List2)
rownames(DF)<-NULL

It will produce this, where scores and sessions are separated by _ :它将产生这个,其中分数和会话由_分隔:

  group community site patientid score1_1 score1_2 score1_3 score2_1 score2_2 score2_3 score3_1 score3_2
1     1         x    a         1       29       79       41       46       96       45       33       95
2     1         x    b         2       88       94        5       68       57       10       69       64
3     1         y    c         3       53       89       55       90       25        4       66       71

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

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