Reputation:
I have a data like this
ldf <- list(structure(list(Abund = c("BROS", "KIS", "TTHS",
"MKS"), `Value: F111: cold, Sample1` = c("1.274e7", "",
"", "2.301e7"), `Value: F111: warm, Sample1` = c("", "",
"", "")), .Names = c("Abund", "Value: F111: cold, Sample1",
"Value: F111: warm, Sample1"), row.names = c(NA, 4L), class = "data.frame"),
structure(list(Abund = c("BROS", "TMS", "KIS",
"HERS"), `Value: F216: cold, Sample2` = c("1.670e6",
"4.115e7", "", "1.302e7"), `Value: F216: warm, Sample2` = c("",
"2.766e7", "", "1.396e7")), .Names = c("Abund", "Value: F216: cold, Sample2",
"Value: F216: warm, Sample2"), row.names = c(NA, 4L), class = "data.frame"),
structure(list(Abund = c("BROS", "TMS", "KIS",
"HERS"), `Value: F655: cold, Sample3` = c("7.074e4",
"1.038e7", "", "7.380e5"), `Value: F655: warm, Sample3` = c("",
"6.874e6", "", "7.029e5")), .Names = c("Abund", "Value: F655: cold, Sample3",
"Value: F655: warm, Sample3"), row.names = c(NA, 4L), class = "data.frame"))
I want to make a unique name in this case Abund then I try to put the data close to it as follows so a desire output is like this
Abund coldsample1 Sample1 coldSample2 warmSample2 coldSample3 warmSample3
BROS 1.27E+07 1.67E+06 7.07E+04
TMS 4.12E+07 2.77E+07 1.04E+07 6.87E+06
HERS 1.30E+07 1.40E+07 7.38E+05 7.03E+05
MKS 2.30E+07
KIS
TTHS
Upvotes: 2
Views: 88
Reputation: 3200
With a bit of help from data.table
you can do (after edit works on basis of lists of data frames of different lengths)
x <- rbindlist(lapply(ldf, function(i) cbind(i["Abund"], stack(i[2:3]), row.names = NULL))) #create a data table with all the unique values of the Abund accrosss the 3 different list of data.frames you provided
y <- dcast(x, Abund~ind, value.var="values") #cast the long format data into a usasble form
names(y) <- gsub(".*:", "", names(y)); names(y) <- gsub(", ", "", names(y)) #get nicer variable names
(y <- y[,lapply(.SD,function(j){ifelse(j=="", NA, j)})]) #prints the end table with a correct and complete list of NAs
# Abund coldSample1 warmSample1 coldSample2 warmSample2 coldSample3 warmSample3
#1: BROS 1.274e7 NA 1.670e6 NA 7.074e4 NA
#2: HERS NA NA 1.302e7 1.396e7 7.380e5 7.029e5
#3: KIS NA NA NA NA NA NA
#4: MKS 2.301e7 NA NA NA NA NA
#5: TMS NA NA 4.115e7 2.766e7 1.038e7 6.874e6
#6: TTHS NA NA NA NA NA NA
Upvotes: 0
Reputation: 18425
In Base R, you could do something like this...
#if you have a dataframe (in the original version of this question): create a list of dataframes by splitting every three columns, and setting the column names as required...
dflist <- lapply(1:3,function(i) {
df <- ldf[,(3*i-2):(3*i)]
names(df) <- c("Abund",paste0("ColdSample",i),paste0("WarmSample",i))
return(df)})
#merge these together
dfout <- Reduce(function(x,y) merge(x,y,all=TRUE), dflist)
#if ldf is a list of dataframes (in the modified version of the question), you can just do
dfout <- Reduce(function(x,y) merge(x,y,all=TRUE), ldf)
#and perhaps tidy up the names with
names(dfout) <- make.names(names(dfout))
dfout
Abund ColdSample1 WarmSample1 ColdSample2 WarmSample2 ColdSample3 WarmSample3
1 BROS 1.274e7 1.670e6 7.074e4
2 HERS <NA> <NA> 1.302e7 1.396e7 7.380e5 7.029e5
3 KIS
4 MKS 2.301e7 <NA> <NA> <NA> <NA>
5 TMS <NA> <NA> 4.115e7 2.766e7 1.038e7 6.874e6
6 TTHS <NA> <NA> <NA> <NA>
Upvotes: 1