R - 如何根据两列之间的关系重塑数据帧?

时间:2016-04-22 18:48:54

标签: r dataframe reshape

我有一个数据框public class Rootobject { public Result[] results { get; set; } public string status { get; set; } } public class Result { public Address_Components[] address_components { get; set; } public string formatted_address { get; set; } public Geometry geometry { get; set; } public string place_id { get; set; } public string[] types { get; set; } } public class Geometry { public Location location { get; set; } public string location_type { get; set; } public Viewport viewport { get; set; } } public class Location { public float lat { get; set; } public float lng { get; set; } } public class Viewport { public Northeast northeast { get; set; } public Southwest southwest { get; set; } } public class Northeast { public float lat { get; set; } public float lng { get; set; } } public class Southwest { public float lat { get; set; } public float lng { get; set; } } public class Address_Components { public string long_name { get; set; } public string short_name { get; set; } public string[] types { get; set; } }

df

我想要做的是将每个MRN的ICD值放入他们自己的列中。如果MRN只有一个与之关联的ICD,那就没关系,但是如果有多个值,它们应该进入他们自己的列。

这样的事情:

ICD    MRN      Adm        
 X     24     1/01/2015      
 Y     21     1/22/2015      
 A     10     2/09/2015      
 B     10     2/09/2015      
 C     10     2/09/2015      
 F     97     4/10/2015      
 X     97     4/10/2015      
 Z     33     5/17/2015      

我无法弄清楚如何做到这一点!我甚至很难问这个问题!我怀疑它需要使用MRN ICD.1 ICD.2 ICD.3 Adm 24 X NA NA 1/01/2015 21 Y NA NA 1/22/2015 10 A B C 2/09/2015 97 F X NA 4/10/2015 33 Z NA NA 5/17/2015 ,但我在这方面很弱,并且会喜欢社区的帮助。

1 个答案:

答案 0 :(得分:2)

这是一种方式 - 在某处复制......

library(tidyr)
library(dplyr)

df %>%
  group_by(MRN) %>%
  mutate(ICD_lbl = paste0("ICD.", data.table::rleid(ICD))) %>%
  spread(ICD_lbl, ICD)