R 需要使列显示哪些ID分配给这些列…不知道如何描述

R 需要使列显示哪些ID分配给这些列…不知道如何描述,r,dataframe,reshape,R,Dataframe,Reshape,每个病人分配给两名医生。总共有三位医生。我的数据如下所示: >df Dr1 Dr2 PatientID Chris John 5 John Mike 24 Mike John 28 我想要的是3列,每个医生一列,显示他们的病人是谁 Chris John Mike 5 5 24 24 28 28 我正在尝试使用melt,但没有任何运气。创建具有不规则列(即不同长度的列)的数据帧有点棘手

每个病人分配给两名医生。总共有三位医生。我的数据如下所示:

>df
Dr1    Dr2    PatientID
Chris  John   5
John   Mike   24
Mike   John   28
我想要的是3列,每个医生一列,显示他们的病人是谁

Chris   John   Mike
5       5      24
        24     28
        28

我正在尝试使用melt,但没有任何运气。

创建具有不规则列(即不同长度的列)的数据帧有点棘手,但这里有一个尝试。注意使用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

创建包含不规则列(即不同长度的列)的数据帧有点棘手,但这里有一个尝试。注意使用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

数据帧是矩形的。您想要的不是矩形,因此让我们列一个列表:

使用这些数据:

df = read.table(text = "Dr1    Dr2    PatientID
Chris  John   5
John   Mike   24
Mike   John   28", header = T)

数据帧是矩形的。您想要的不是矩形,因此让我们列一个列表:

使用这些数据:

df = read.table(text = "Dr1    Dr2    PatientID
Chris  John   5
John   Mike   24
Mike   John   28", header = T)

类似于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
资料


类似于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
资料


哇!太酷了!我注意到的唯一一件事是,在再次显示患者分配之前,有时会有许多行仅NAs。通常我会使用df.new,很难说没有产生问题的示例数据。同样,对于这些数据,列表对象可能比包含大量NA值的数据框更好,但是您专门请求了一个数据框。哇!太酷了!我注意到的唯一一件事是,在再次显示患者分配之前,有时会有许多行仅NAs。通常我会使用df.new,很难说没有产生问题的示例数据。同样,对于这些数据,列表对象可能比具有大量NA值的数据帧更好,但是您专门请求了一个数据帧。