我有一部分数据:
a3 <- structure(list(Employee.Number = c("10034", "10034", "10034"),
Employee.Full.Name = c("Wayne, Bruce", "Wayne, Bruce",
"Wayne, Bruce"), Case.No = c("78582", "87758", "87758"),
Diagnoses.No = c("1", "2", "1"), Diagnosis.Medical.Code.Code = c("151.9",
"285.9", "V70-V82"), Diagnosis.Medical.Code.Description = c("Stomach, unspecified",
"Anemia, unspecified", "Tummy AChe"
)), row.names = c(NA, -3L), class = "data.frame")
如何对Case.No进行分组,如果“ Diagnoses.No”等于2(实际数据的数字> 2),则最后3列将粘贴到同一Case.no的列上诊断,否,将原始列删除为1?
所需结果:
10034 Wayne, Bruce 78582 1 151.9 Stomach, unspecified NA
10034 Wayne, Bruce 87758 1 V70-V82 Tummy AChe 2 285.9 Anemia, unspecified
答案 0 :(得分:0)
如果我理解正确,OP希望将多个值列从长格式更改为宽格式。
一种可能的方法是在基数R中使用reshape()
函数:
reshape(a3, v.names = c("Diag.MC.Code", "Diag.MC.Descr"), timevar = "Diag.No",
idvar = c("Emp.No", "Emp.Full.Name", "Case.No"), direction = "wide")
Emp.No Emp.Full.Name Case.No Diag.MC.Code.1 Diag.MC.Descr.1 Diag.MC.Code.2 Diag.MC.Descr.2 1 10034 Wayne, Bruce 78582 151.9 Stomach, unspecified <NA> <NA> 2 10034 Wayne, Bruce 87758 V70-V82 Tummy AChe 285.9 Anemia, unspecified
请注意,列名已重命名(缩短)以使结果表适合页面。
a3 <- structure(list(Emp.No = c("10034", "10034", "10034"), Emp.Full.Name = c("Wayne, Bruce",
"Wayne, Bruce", "Wayne, Bruce"), Case.No = c("78582", "87758",
"87758"), Diag.No = c("1", "2", "1"), Diag.MC.Code = c("151.9",
"285.9", "V70-V82"), Diag.MC.Descr = c("Stomach, unspecified",
"Anemia, unspecified", "Tummy AChe")), row.names = c(NA, -3L), class = "data.frame")
# abbreviate column names to make result table fit on page
a3 <- setNames(a3, c("Emp.No", "Emp.Full.Name", "Case.No", "Diag.No",
"Diag.MC.Code", "Diag.MC.Descr"))
答案 1 :(得分:0)
演示文稿与您想要的内容有些不同,但是请看它是否适合您。
tidyverse
解决方案:
# function to concartinate columns
cat_function <- function(df){
map_df(df, ~paste(unlist(.), collapse = " / "))
}
a3 %>%
group_by(Employee.Number, Employee.Full.Name, Case.No) %>%
nest %>%
transmute(Employee.Number, Employee.Full.Name, Case.No,
res = map(data, cat_function)) %>%
unnest
答案 2 :(得分:0)
我不确定所需的输出,但是我认为此脚本可以提供帮助。它使用data.table包:
library(data.table)
a3<-data.table(a3)
a2 <-a3[Diagnoses.No==2,list(Employee.Number,Employee.Full.Name,Case.No,
x=paste0(Diagnoses.No, ' ', Diagnosis.Medical.Code.Code), Diagnosis.Medical.Code.Description),]
a1 <-a3[Diagnoses.No==1,]
a3 <- merge(a1,a2, by=c('Employee.Number','Employee.Full.Name','Case.No'), all.x=T)
a3 <- a3[,list(Employee.Number,Employee.Full.Name,Case.No,Diagnosis.Medical.Code.Code,
Diagnosis.Medical.Code.Description.x= paste0(Diagnosis.Medical.Code.Description.x,' ',x),
Diagnosis.Medical.Code.Description.y)]
输出类似于您的请求:
> a3
Employee.Number Employee.Full.Name Case.No Diagnosis.Medical.Code.Code Diagnosis.Medical.Code.Description.x Diagnosis.Medical.Code.Description.y
1: 10034 Wayne, Bruce 78582 151.9 Stomach, unspecified NA <NA>
2: 10034 Wayne, Bruce 87758 V70-V82 Tummy AChe 2 285.9 Anemia, unspecified
希望有帮助!