-1

I have a Data frame as

id dep    e1  e2  e3  
 1  x     a   b   c
 2  y     d   e   f
 3  z     g   h   i

Now I want to create a column (not in same table) which only consist data like this

edu

a
b
c
d
e
f
g
h
i

please help me how I can do it R ?

Sotos
  • 47,396
  • 5
  • 31
  • 61
girijesh96
  • 425
  • 1
  • 3
  • 15
  • related: https://stackoverflow.com/questions/2185252/reshaping-data-frame-from-wide-to-long-format – jogo Feb 23 '18 at 08:21

2 Answers2

1
c(t(dat[3:5]))
[1] "a" "b" "c" "d" "e" "f" "g" "h" "i"

If you want is as data.frame:

data.frame(edu=c(t(dat[3:5])))
  edu
1   a
2   b
3   c
4   d
5   e
6   f
7   g
8   h
9   i
onyambu
  • 49,350
  • 3
  • 19
  • 45
0

You can use data.table:

library(data.table)

Coerce the data.frame to data.table:

setDT(df)

Melt it from wide to long, sort by value and subset for that column:

melt(df, id.vars= c("id", "dep"))[order(value), ][, .(value)]

The result is a data.table with 1 column and 9 observations:

   value
1:     a
2:     b
3:     c
4:     d
5:     e
6:     f
7:     g
8:     h
9:     i

If you need a data.frame just use setDF.

clemens
  • 6,224
  • 2
  • 16
  • 27