I have data consisting of many columns/variables and three rows. Each variable is an integer and the values vary across rows and columns. Below is a minimal example of my data:
# Minimal example of data frame I have
df <- data.frame(x1 = c(1,2,3),
x2 = c(4,1,6),
x3 = c(3,0,2),
x4 = c(3,0,1))
I am trying to somehow collapse each column into a numeric vector containing the values in each row. For example, I want something like:
# Desired data based on minimal example
target_list <- list(c(1,2,3),
c(4,1,6),
c(3,0,2),
c(3,0,1))
The ultimate goal is to be able to take another data frame with many columns and generate a new data frame containing only the columns with indices matching the values in each numeric vector. For each vector, I generate another data frame. All frames are stored in a list. An example of my target output given the working example inputs:
# Example "super data frame" I will subset. The values contained in each column are arbitrary.
df2 <- data.frame(z1 = "a", z2 = "b",
z3 = 999, z4 = NA,
z5 = "foo", z6 = "bar")
# Subset "super data frame" to only columns in each vector in the list, store in a list
list(df2[ ,target_list[[1]]],
df2[ ,target_list[[2]]],
df2[ ,target_list[[3]]],
df2[ ,target_list[[4]]])
I've tried various pasting approaches, but they produce character vectors that I can't use to select the columns of the other data frame by index, e.g. it produces this:
paste0(df[1, ], df[2, ], df[3, ], df[4, ])
Any help on how to generate the list of numeric vectors from df?