1

I have a dataframe in R of x columns where the first column (C1) contains species names and further column their abundances per samples. I would like to split that dataframe where each new splitted dataframe includes C1 + Cx. Therefore if there was 20 columns, there would be 19 dataframes with C1+C2, C1+C3,C1+C4, etc.

I have been able to split the different columns using lapply and split, but i can't find a way to attach to each of those splitted frames the column C1. Would any have suggestions? Thanks!

1 Answers1

2

Here is a way using split.default and lapply

# input data
iris2 <- iris[, c(5, 1:4)]

We split data column-wise excluding the first column, this returns a list.

iris2_split <- split.default(iris2[-1], names(iris2)[-1])

Then we use lapply and cbind to attach the first column of iris2 the every entry in iris2_split.

out <- lapply(iris2_split, cbind, iris2[1])

Result (truncated)

lapply(out, head, 3)
#$Petal.Length
#  Petal.Length Species
#1          1.4  setosa
#2          1.4  setosa
#3          1.3  setosa

#$Petal.Width
#  Petal.Width Species
#1         0.2  setosa
#2         0.2  setosa
#3         0.2  setosa

#$Sepal.Length
#  Sepal.Length Species
#1          5.1  setosa
#2          4.9  setosa
#3          4.7  setosa

#$Sepal.Width
#  Sepal.Width Species
#1         3.5  setosa
#2         3.0  setosa
#3         3.2  setosa
markus
  • 25,843
  • 5
  • 39
  • 58
  • Thanks! I managed furthermore to create multiple tables using list2env. My last struggle now is to export all these tables. Would you have a way to loop write all those created tables into txt files (One txt file per table)? Thanks again for your fast help! – Vincent La Foote Carrier Nov 05 '18 at 11:00
  • Glad it worked. Check this post: [Export a list into a CSV or TXT file in R](https://stackoverflow.com/questions/27594541/export-a-list-into-a-csv-or-txt-file-in-r) – markus Nov 05 '18 at 11:02
  • Thanks again for your recommendation. I did try a similar way, however it saves under one file. Whereas I would need one txt file per table in the list. Is it just about removing the append = T? – Vincent La Foote Carrier Nov 05 '18 at 11:13
  • Can you try something like `Map(f = write.csv, x = out, file = paste0("path/to/file/", names(out), ".csv")` – markus Nov 05 '18 at 11:16
  • Hi I tried the method, however I get in the console a list of all samples with NULL under: Map(f = write.csv, x = out, file = paste0("file/path/", names(out), ".csv")) $`GC1048d305cm` NULL $GC1070d57cm NULL $X1029d14to15cm NULL $X1029d1to3cm NULL $X1029d4to5cm NULL $X1029d8to10cm NULL – Vincent La Foote Carrier Nov 05 '18 at 12:15
  • Or if I use the example data I also end with same results: Map(f = write.csv, x = out, file = paste0("path/file/", names(out), ".csv")) $`Petal.Length` NULL $Petal.Width NULL $Sepal.Length NULL $Sepal.Width NULL – Vincent La Foote Carrier Nov 05 '18 at 13:33
  • Finally I used for(i in seq_along(out)){ write.table(out[[i]], file=paste0(filepath', names(out)[i], '.txt'),row.names = FALSE, quote=FALSE) } And all worked. Thanks again! – Vincent La Foote Carrier Nov 05 '18 at 16:00