I have a dataframe df
that has many cols and say 100 rows.
How do I take all the level values from the columns with names "alpha", "gamma" and "zeta" and store the 300 of them in a single vector?
I have a dataframe df
that has many cols and say 100 rows.
How do I take all the level values from the columns with names "alpha", "gamma" and "zeta" and store the 300 of them in a single vector?
I've found that converting to a matrix first makes getting to levels a bit easier.
as.vector(as.matrix(df[,c("alpha", "gamma", "zeta")]))
Of course, you could have just done stringsAsFactors=FALSE
when you read the data in initially.
You have an accepted answer, but here's what I think is happening: You have a combination of factor
and character
columns. In that case, unlist
doesn't work directly, but if they were all factor
or if they were all character
, there would be no problem:
Some sample data:
mydf <- data.frame(A = LETTERS[1:3], B = LETTERS[4:6], C = LETTERS[7:9],
D = LETTERS[10:12], E = LETTERS[13:15])
df <- mydf
df$E <- as.character(df$E)
colsOfInterest <- c("A", "B", "E")
unlist(mydf[colsOfInterest], use.names = FALSE)
# [1] A B C D E F M N O
# Levels: A B C D E F M N O
unlist(df[colsOfInterest], use.names = FALSE)
# [1] "1" "2" "3" "1" "2" "3" "M" "N" "O"
unlist(lapply(df[colsOfInterest], as.character), use.names = FALSE)
# [1] "A" "B" "C" "D" "E" "F" "M" "N" "O"
For a problem at the scale described here, the benchmarks show that converting to character first and using unlist
is actually the fastest approach if you don't care for retaining factors. Note that the result of fun1()
won't be correct if some columns are factors and some are characters. Here's a benchmark on a 100 row data.frame
:
library(microbenchmark)
microbenchmark(fun1(), fun2(), fun3())
# Unit: microseconds
# expr min lq median uq max neval
# fun1() 572.606 587.3595 595.4845 606.175 3439.055 100
# fun2() 327.570 334.6265 341.2550 350.449 3443.758 100
# fun3() 1037.020 1055.6215 1064.1745 1086.197 3929.981 100
Of course, here we're talking microseconds, but the results scale too.
For reference, here's what was used for benchmarking. Change "nRow
" and "nCol
" if you want to test on a different sized data.frame
extracting different numbers of columns.
nRow <- 100
nCol <- 30
set.seed(1)
mydf <- data.frame(matrix(sample(LETTERS, nRow*nCol, replace = TRUE), nrow = nRow))
colsOfInterest <- sample(nCol, sample(nCol*.7, 1))
length(colsOfInterest)
# [1] 17
library(microbenchmark)
fun1 <- function() unlist(mydf[colsOfInterest], use.names = FALSE)
fun2 <- function() unlist(lapply(mydf[colsOfInterest], as.character), use.names = FALSE)
fun3 <- function() as.vector(as.matrix(mydf[colsOfInterest]))
microbenchmark(fun1(), fun2(), fun3())
vec <- unlist(lapply( df[ ,
names(df) %in% c("alpha","gamma", "zeta") ],
levels) )[1:300]
That would give the unique levels. If you want the first 300 values in those columns, do this:
vec <- unlist(lapply( df[ ,
names(df) %in% c("alpha","gamma", "zeta") ],
as.character) )[1:300]
unlist(df[c("the","cols","you","want")], use.names = FALSE)
? – A5C1D2H2I1M1N2O1R2T1 Dec 12 '13 at 5:57data.frame
that demonstrates your input and avector
that shows your desired output. When I useunlist
as demonstrated above, thefactors
remained asfactors
unless one of the columns was acharacter
column. – A5C1D2H2I1M1N2O1R2T1 Dec 12 '13 at 6:35