Reputation: 4232
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?
Upvotes: 1
Views: 660
Reputation: 388972
Or use as.list
as.list(df)
#$x1
#[1] 1 2 3
#$x2
#[1] 4 1 6
#$x3
#[1] 3 0 2
#$x4
#[1] 3 0 1
You can use unname
to remove names of the list.
Upvotes: 3
Reputation: 132706
Maybe I'm missing something, but the only difference between your input and your target are three attributes:
attributes(df)
#$names
#[1] "x1" "x2" "x3" "x4"
#
#$class
#[1] "data.frame"
#
#$row.names
#[1] 1 2 3
You can remove them:
attributes(df) <- NULL
df
#[[1]]
#[1] 1 2 3
#
#[[2]]
#[1] 4 1 6
#
#[[3]]
#[1] 3 0 2
#
#[[4]]
#[1] 3 0 1
Or, alternatively:
c(unname(unclass(df)))
But, of course, these attributes don't hurt and you can always treat a data.frame like a list (because it actually is a list).
Upvotes: 2