每位患者被分配给两名医生。一共有三位医生。我的数据如下:
>df
Dr1 Dr2 PatientID
Chris John 5
John Mike 24
Mike John 28
我想要的是3列(每位医生一列),显示他们的患者是谁
Chris John Mike
5 5 24
24 28
28
我试图玩melt(),但没有运气。
答案 0 :(得分:2)
创建具有参差不齐的列(即,不同长度的列)的数据框架有些棘手,但这是一种尝试。请注意magrittr的%$%
运算符的使用:
library(tidyverse)
df <- read.table(text = 'Dr1 Dr2 PatientID
Chris John 5
John Mike 24
Mike John 28', header = T)
list.per.dr <- df %>%
gather(doc, name, -PatientID) %>%
select(-doc) %$%
split(PatientID, name)
$Chris
[1] 5
$John
[1] 24 5 28
$Mike
[1] 28 24
我们现在有了一个列表对象,其中列出了分配给每个医生的患者。要将其转换为数据帧,我们需要均衡其长度:
max_patients <- max(lengths(list.per.dr))
df.new <- list.per.dr %>%
lapply(function(x) c(x, rep(NA, max_patients - length(x)))) %>%
as.data.frame()
Chris John Mike
1 5 24 28
2 NA 5 24
3 NA 28 NA
答案 1 :(得分:2)
数据框为矩形。您想要的不是矩形,所以让我们制作一个list
:
with(reshape2::melt(df, id.vars = "PatientID"), split(PatientID, value))
# $Chris
# [1] 5
#
# $John
# [1] 24 5 28
#
# $Mike
# [1] 28 24
使用此数据:
df = read.table(text = "Dr1 Dr2 PatientID
Chris John 5
John Mike 24
Mike John 28", header = T)
答案 2 :(得分:2)
类似于Gregor解决方案的基本R选项
unstack(reshape(dat, idvar = "PatientID", varying = 1:2, direction = "long", sep = ""),
PatientID ~ Dr)
# $Chris
# [1] 5
#
# $John
# [1] 24 5 28
#
# $Mike
# [1] 28 24
数据
text <- "Dr1 Dr2 PatientID
Chris John 5
John Mike 24
Mike John 28"
dat <- read.table(text = text, stringsAsFactors = FALSE, header = TRUE)