我有一个数据集
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))
我正在尝试运行这些代码,以获得不同分数列的类似输出,如果可能的话,几行代码将通过某些列表或其他命令运行不同组、社区和站点的代码
在这方面的任何帮助将是可观的。
使用类似于问题中发布的虚拟数据,您可以创建基于group
,community
和的索引site
。将其拆分为一个列表,为所有分数应用任务,然后将所有分数绑定在一起。我添加了一个函数来做到这一点:
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
它将产生这个,其中分数和会话由以下分隔_
:
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
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句