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)
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)
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With