Reputation: 2583
I have a data frame like this:
COL1 COL2 COL3
a h
b f
c g
d j
I would like the following output:
COL
a
b
c
d
f
g
h
j
How this can be done? Thanks in advance!
Upvotes: 3
Views: 7935
Reputation: 81693
You could use unlist
:
dat <- data.frame(A1 = letters[1:3],
A2 = letters[2:4],
A3 = c("f", "a", "b"), stringsAsFactors = FALSE)
unlist(dat)
# A11 A12 A13 A21 A22 A23 A31 A32 A33
# "a" "b" "c" "b" "c" "d" "f" "a" "b"
Upvotes: 3
Reputation: 61933
do.call(c, your_data_frame)
An example:
> dat <- data.frame(a = 1:3, b = 2:4)
> do.call(c, dat)
a1 a2 a3 b1 b2 b3
1 2 3 2 3 4
This takes each column of the data.frame and treats it as a separate input into c so you're just concatenating everything into a single vector.
Upvotes: 4
Reputation: 193527
If your data.frame
is named dat
, try:
unlist(dat, use.names=FALSE)
You may also want to try the following:
as.character(unlist(dat, use.names=FALSE))
c(na.omit(as.character(unlist(dat, use.names=FALSE))))
which might be useful if your data are entered as factors.
Also, in your question, you make it appear that you don't actually have a basic "vector", but rather a one-column data.frame
. Thus, something like dat2 <- data.frame(COL = c(na.omit(as.character(unlist(dat, use.names=FALSE)))))
might be what you're looking for.
Upvotes: 9