Home > Mobile >  Creating a .csv file from three seperate list
Creating a .csv file from three seperate list

Time:11-29

Using the two list from below I would like to export a .csv file that has the values from l2 and l3 in their own seperate columns. Within those unique names in l there is a letter (e.g., A, B, C) and there is a number (e.g., 2001, 2002, 2003). I would to partition them so that the letters have its own column, and the number has a seperate column.

Expected Output:

Letter Number Values from l2 Values from l3
A 2001 10 15
B 2002 11 16
C 2003 12 17
l <- list("A_2001","A_2001","B_2002","B_2002", "C_2003", "C_2003")

l2 <- list("1"= 10,
           "2" = 11,
           "3" = 12)

l3 <- list("1"= 15,
           "2" = 16,
           "3" = 17)

CodePudding user response:

Based on the input, we may unlist the list elements and create the data.frame with read.table and transform - get the unique elements of 'l', unlist, read with read.table specifying the _ as sep to create a two column data.frame, then unlist the 'l2' and 'l3' to add new columns in the dataset

out <- transform(read.table(text = unlist(unique(l)), header = FALSE, 
    col.names = c("Letter", "Number"), sep = "_"),
    Values_from_l2 = unlist(l2), Values_from_l3 = unlist(l3))

-output

out
  Letter Number Values_from_l2 Values_from_l3
1      A   2001             10             15
2      B   2002             11             16
3      C   2003             12             17
  • Related