简体   繁体   中英

Create and fill new table with existing data in R

Using R, how do I take several tables of results each with differing results columns and combine them row wise such that all results are captured, with NAs or blanks if a set of results doesn't have this column. Essentially I need to take data I have

我有的数据

and turn it into data I want

我想要的数据

Note that I don't care about Brand Model and Year, they can just be stacked on top of each other.

Apologies for the poorly formatted post, I'm still finding my feet around here.

We can use rbindlist from data.table after keeping it in a list

library(data.table)
rbindlist(list(df1, df2, df3), use.names = TRUE, fill=TRUE)

or use bind_rows from dplyr

library(dplyr)
bind_rows(df1, df2, df3)

Update

In case, the data is in a single file as showed in the image, read it with readLines , then split it to a list and use rbindlist

lines1 <- trimws(readLines("temp1.csv"))
i1 <- cumsum(grepl("^Brand", lines1))
i2 <- lines1!=''
lst <- lapply(split(lines1[i2], i1[i2]), 
       function(x) read.csv(text=x, sep=""))
rbindlist(lst, use.names=TRUE, fill = TRUE)

Or

bind_rows(lst)

data

df1 <- data.frame(Brand = 1, Model ="A", Year = 2010:2014, 
              Dogs = c(0.71, 0.76, 0.40, 0.39, 0.67),
              Cats = c(0.64,0.06,0.18, 0.20, 0.23),
        Rabbits = c(0.56, 0.96, 0.90, 0.38, 0.73),
               stringsAsFactors=FALSE)

df2 <- data.frame(Brand = 1, Model ="B", Year = c(2010, 2011, 2013, 2014), 
              Dogs = c(0.12, 0.43, 0.79, 0.29),
              Ducks = c(0.67, 0.48, 0.80, 0.70),
               stringsAsFactors=FALSE)

df3 <-  data.frame(Brand = 1, Model ="C", Year = 2013:2014, 
              Dogs = c(0.76, 0.98),
              Cats = c(0.90, 0.84),
        Lions = c(0.12, 0.22),
              Wolves = c(0.75, 0.61),
               stringsAsFactors=FALSE)

Using merge() :

> Reduce(function(x, y) merge(x, y, all=TRUE), list(df1, df2, df3))
   Brand Model Year Dogs Cats Rabbits Ducks Lions Wolves
1      1     A 2010 0.71 0.64    0.56    NA    NA     NA
2      1     A 2011 0.76 0.06    0.96    NA    NA     NA
3      1     A 2012 0.40 0.18    0.90    NA    NA     NA
4      1     A 2013 0.39 0.20    0.38    NA    NA     NA
5      1     A 2014 0.67 0.23    0.73    NA    NA     NA
6      1     B 2010 0.12   NA      NA  0.67    NA     NA
7      1     B 2011 0.43   NA      NA  0.48    NA     NA
8      1     B 2013 0.79   NA      NA  0.80    NA     NA
9      1     B 2014 0.29   NA      NA  0.70    NA     NA
10     1     C 2013 0.76 0.90      NA    NA  0.12   0.75
11     1     C 2014 0.98 0.84      NA    NA  0.22   0.61

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM