0

I would like to take each rows of a data.frame, a [45,6] data.frame and make one continuous column vector with out have to write each index, such as; data.frame1 <- t(data.frame[1,2:6]), I just want the last five columns, then combining with rbind. Is there an R function for this, or is a loop function capable? Note: my loop skills are not so good.

Thanks Kirk

UseR10085
  • 7,120
  • 3
  • 24
  • 54
Kirk
  • 1
  • 1
  • That task is relatively obscure, I find it hard to imagine it being common enough (given the high variability of `data.frame` composition) that this would be a base-R kind of thing. You very clearly gave the functions you need: `t` and `rbind`. how more direct do you need? – r2evans Feb 15 '20 at 04:46
  • But if you want more help, this question is lacking much detail. Please make this question *reproducible*. This includes sample code (including listing non-base R packages), sample *unambiguous* data (e.g., `dput(head(x))` or `data.frame(x=...,y=...)`), and expected output. Refs: https://stackoverflow.com/questions/5963269, https://stackoverflow.com/help/mcve, and https://stackoverflow.com/tags/r/info. – r2evans Feb 15 '20 at 04:47

2 Answers2

0

Probably use the apply function. If the data frame (df) has only 6 columns, you can just omit the first column with df[,-1]. Then apply the rbind function to every row (MARGIN=1) and combine the values into a vector with the c function.

c(apply(df[,-1], MARGIN=1, rbind))

Reproducible example

Using the mtcars data:

> df <- mtcars[1:6]
> head(df)
                   mpg cyl disp  hp drat    wt
Mazda RX4         21.0   6  160 110 3.90 2.620
Mazda RX4 Wag     21.0   6  160 110 3.90 2.875
Datsun 710        22.8   4  108  93 3.85 2.320
Hornet 4 Drive    21.4   6  258 110 3.08 3.215
Hornet Sportabout 18.7   8  360 175 3.15 3.440
Valiant           18.1   6  225 105 2.76 3.460

> x <- c(apply(df[,-1], 1, rbind))
> head(x,20)
 [1]   6.000 160.000 110.000   3.900   2.620   6.000 160.000 110.000   3.900   2.875
[11]   4.000 108.000  93.000   3.850   2.320   6.000 258.000 110.000   3.080   3.215
Edward
  • 10,360
  • 2
  • 11
  • 26
0

Here's a solution that seems to work, using tto transpose columns into rows (and vice versa) and unlistto append multiple columns into a single vector:

DATA:

df <- mtcars[,1:6] 

SOLUTION:

df_t <-t(df[,2:6])
as.numeric(unlist(df_t))

RESULT:

  [1]   6.000 160.000 110.000   3.900   2.620   6.000 160.000 110.000   3.900   2.875   4.000 108.000  93.000
 [14]   3.850   2.320   6.000 258.000 110.000   3.080   3.215   8.000 360.000 175.000   3.150   3.440   6.000
 [27] 225.000 105.000   2.760   3.460   8.000 360.000 245.000   3.210   3.570   4.000 146.700  62.000   3.690
 [40]   3.190   4.000 140.800  95.000   3.920   3.150   6.000 167.600 123.000   3.920   3.440   6.000 167.600
 [53] 123.000   3.920   3.440   8.000 275.800 180.000   3.070   4.070   8.000 275.800 180.000   3.070   3.730
 [66]   8.000 275.800 180.000   3.070   3.780   8.000 472.000 205.000   2.930   5.250   8.000 460.000 215.000
 [79]   3.000   5.424   8.000 440.000 230.000   3.230   5.345   4.000  78.700  66.000   4.080   2.200   4.000
 [92]  75.700  52.000   4.930   1.615   4.000  71.100  65.000   4.220   1.835   4.000 120.100  97.000   3.700
[105]   2.465   8.000 318.000 150.000   2.760   3.520   8.000 304.000 150.000   3.150   3.435   8.000 350.000
[118] 245.000   3.730   3.840   8.000 400.000 175.000   3.080   3.845   4.000  79.000  66.000   4.080   1.935
[131]   4.000 120.300  91.000   4.430   2.140   4.000  95.100 113.000   3.770   1.513   8.000 351.000 264.000
[144]   4.220   3.170   6.000 145.000 175.000   3.620   2.770   8.000 301.000 335.000   3.540   3.570   4.000
[157] 121.000 109.000   4.110   2.780
Chris Ruehlemann
  • 20,321
  • 4
  • 12
  • 34