【问题标题】:Unlist list of data frames to one data frame with change structure将数据帧列表取消列出到一个具有更改结构的数据帧
【发布时间】:2021-02-15 04:05:35
【问题描述】:

我有一个包含大量数据框的列表(每个数据框代表一年)。每个数据框由两个变量组成:(1) ID 变量,和 (2) 表示当年是否发生事件的伯努利变量。我想取消列出该列表,而是创建一个数据框,其中每年的结果变量是它自己的列。

您可以在下面找到生成示例数据的代码,这些示例数据与我的数据具有相同的特征。

set.seed(123)
df <- list()
year <- (1950:1960)

# Simulating data. 
for (i in 1:length(year)) {
  id <- sample(1:20, 10)
  outcome <- sample(0:1, 10, replace = T)
  
  df[[i]] <- cbind(id, outcome)
}

所以实际上,我想取消列出 df 并创建一个看起来像这样的数据框:

      id outcome1950 outcome1951 outcome1952 outcome1953 outcome1954 outcome1955 [...]
 [1,]  1               
 [2,]  2       
 [3,]  3       
 [4,]  4       
 [5,]  5       
 [6,]  6       
 [7,]  7       
 [8,]  8       
 [9,]  9       
[10,] 10       
[11,] 11       
[12,] 12       
[13,] 13       
[14,] 14       
[15,] 15       
[16,] 16       
[17,] 17       
[18,] 18       
[19,] 19       
[20,] 20       

当然,结果应该具有每年的每个值。

注意:并非所有年份都存在所有 ID,在这种情况下,我想添加一个 NA

【问题讨论】:

  • 谢谢,DPH、Roccer 和 DaveArmstrong,您的所有解决方案都成功了。谢谢你拯救了我的一天!
  • 欢迎。顺便提一句。我曾经有一个类似的问题。如果您想要更多选项,可以在这里查看:stackoverflow.com/questions/63613264/…

标签: r list dplyr data-manipulation


【解决方案1】:

应该这样做。

set.seed(123)
df <- list()
year <- (1950:1960)

# Simulating data. 
for (i in 1:length(year)) {
  id <- sample(1:20, 10)
  outcome <- sample(0:1, 10, replace = T)
  
  df[[i]] <- cbind(id, outcome)
}

## add names to df
names(df) <- year

## turn each element in df into a tibble (or data frame) and make a 
## variable called "year" that has is "outcomeYYYY"
for(i in 1:length(df)){
  df[[i]] <- as_tibble(df[[i]]) %>% mutate(year = paste0("outcome", names(df)[i]))
}

## turn the elements of the list into a single data tibble
df <- bind_rows(df)

## pivot to wider and arrange by id
dfwide <- df %>% 
  pivot_wider(names_from="year", values_from="outcome") %>% 
  arrange(id)

head(dfwide)
# A tibble: 6 x 12
#      id outcome1950 outcome1951 outcome1952 outcome1953 outcome1954
#   <int>       <int>       <int>       <int>       <int>       <int>
# 1     1          NA           0          NA           0          NA
# 2     2           1          NA           0           1          NA
# 3     3           1           0          NA          NA           1
# 4     4           0           0          NA          NA          NA
# 5     5           0          NA           0          NA          NA
# 6     6           0          NA           0           1          NA
# … with 6 more variables: outcome1955 <int>, outcome1956 <int>,
#   outcome1957 <int>, outcome1958 <int>, outcome1959 <int>,
#   outcome1960 <int>

【讨论】:

    【解决方案2】:

    如果我正确理解了您的问题,可能的解决方案是:

    library(dplyr)
    library(tidyr)
    library(purrr)
    library(plyr)
    
    purrr::map2(df, year, ~ cbind(.y, .x))  %>% 
      plyr::ldply(data.frame) %>% 
      dplyr::rename(YEAR = 1) %>% 
      tidyr::pivot_wider(names_from = "YEAR", names_prefix = "outcome", values_from = "outcome") %>% 
      dplyr::arrange(id)
    

    【讨论】:

      【解决方案3】:
      library(tidyverse)
      
      names(df) <- paste0('outcome', year)
      
      df %>% 
        purrr::map_df(as.data.frame, .id = 'name') %>% 
        tidyr::pivot_wider(names_from = name, values_from = outcome) %>% 
        dplyr::arrange(id)
      
      # A tibble: 20 x 12
            id outcome1950 outcome1951 outcome1952 outcome1953 outcome1954 outcome1955 outcome1956 outcome1957 outcome1958 outcome1959 outcome1960
         <int>       <int>       <int>       <int>       <int>       <int>       <int>       <int>       <int>       <int>       <int>       <int>
       1     1          NA           0          NA           0          NA           0          NA          NA           0          NA          NA
       2     2           1          NA           0           1          NA           1          NA           1          NA           0          NA
       3     3           1           0          NA          NA           1          NA          NA           0           0          NA          NA
       4     4           0           0          NA          NA          NA           0          NA           1           0           1          NA
       5     5           0          NA           0          NA          NA          NA           1          NA          NA          NA          NA
       6     6           0          NA           0           1          NA           1           1          NA           0           1          NA
       7     7          NA           1           0           0           1          NA           1           0          NA          NA           1
       8     8          NA           0           0           0           1          NA          NA           1           1           0           0
       9     9          NA           0           0          NA           1          NA          NA           1          NA           0           0
      10    10           0           0           0          NA          NA           0           1          NA          NA           1           1
      11    11           0          NA          NA           1          NA          NA           1          NA           1           0          NA
      12    12          NA          NA          NA          NA           0           1          NA          NA           1           0           0
      13    13          NA          NA           0          NA           1          NA          NA           1           0           1           0
      14    14           0           1          NA          NA           0          NA           0          NA          NA           0           0
      15    15           1          NA           1           0          NA           0           1          NA           1          NA          NA
      16    16          NA          NA          NA           0           0           0          NA          NA           0          NA           0
      17    17          NA          NA          NA          NA          NA          NA          NA           1          NA          NA          NA
      18    18          NA          NA          NA          NA          NA          NA           1          NA          NA          NA           1
      19    19           1           0          NA           0           0           1           0           1          NA          NA          NA
      20    20          NA           1           1           1           0           0           0           1          NA          NA           0
      

      【讨论】:

        猜你喜欢
        • 2019-11-16
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-07-28
        • 1970-01-01
        • 2021-07-25
        • 1970-01-01
        • 2017-06-27
        相关资源
        最近更新 更多