0

All I want to do do is combine columns[1:3] with columns[4:6].

The final product would just have 3 long columns with col[4] below col[1], col[5] below col[2], etc.

All the solutions I see depend on ID columns that are non existent in my data. I looked at gather(),stack(), melt(). I just want to simply cut the last 3 columns and paste them below the first 3 columns,

PotterFan
  • 81
  • 2
  • 7
  • 1
    yes, `melt` , `pivot_longer` and `reshape` ca do the work as needed: eg `reshape(df, matrix(1:6,2,byrow = true), dir = "long")` – Onyambu Aug 04 '20 at 20:00
  • You could add an `id`, do the transformation, then remove it. It's easier to help you if you include a simple [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with sample input and desired output that can be used to test and verify possible solutions. – MrFlick Aug 04 '20 at 20:03
  • try somethin like `reshape(df, matrix(1:6,2,byrow = true), dir = "long")` – Onyambu Aug 04 '20 at 20:05

3 Answers3

0

If the columns all contain the same class (i.e. integer), you can simply use the rbind() function to bind columns [4:6] under columns [1:3].

If they have different formats, you can define a new dataset with combined columns:

new.data = data.frame("First"=c(data[,1],data[,4]),
                      "Second"=c(data[,2],data[,5]),
                      "Third"=c(data[,3],data[,6]))

With rbind() you can just add the columns below, as in this example:

> dta = matrix(rep(c(1,2,3,4,5,6),6),ncol=6,byrow=T)
> print(dta)
     [,1] [,2] [,3] [,4] [,5] [,6]
[1,]    1    2    3    4    5    6
[2,]    1    2    3    4    5    6
[3,]    1    2    3    4    5    6
[4,]    1    2    3    4    5    6
[5,]    1    2    3    4    5    6
[6,]    1    2    3    4    5    6
> rbind(dta[,1:3],dta[,4:6])
      [,1] [,2] [,3]
 [1,]    1    2    3
 [2,]    1    2    3
 [3,]    1    2    3
 [4,]    1    2    3
 [5,]    1    2    3
 [6,]    1    2    3
 [7,]    4    5    6
 [8,]    4    5    6
 [9,]    4    5    6
[10,]    4    5    6
[11,]    4    5    6
[12,]    4    5    6
Martin Wettstein
  • 2,771
  • 2
  • 9
  • 15
  • Edited the post to give an example. But mark that this will only work if the object class of all elements is the same. If you have character columns and numeric columns, use the `data.frame()` function to manually compose the dataset. – Martin Wettstein Aug 05 '20 at 16:59
0

Lets assume your data is like below, you could use reshape from base R:

a <- head(cbind(iris[1:3], iris[1:3]))
a
  Sepal.Length Sepal.Width Petal.Length Sepal.Length Sepal.Width Petal.Length
1          5.1         3.5          1.4          5.1         3.5          1.4
2          4.9         3.0          1.4          4.9         3.0          1.4
3          4.7         3.2          1.3          4.7         3.2          1.3
4          4.6         3.1          1.5          4.6         3.1          1.5
5          5.0         3.6          1.4          5.0         3.6          1.4
6          5.4         3.9          1.7          5.4         3.9          1.7

The code to use:

reshape(a, matrix(names(a), 3), dir="long")

    time Sepal.Length Sepal.Width Petal.Length id
1.1    1          5.1         3.5          1.4  1
2.1    1          4.9         3.0          1.4  2
3.1    1          4.7         3.2          1.3  3
4.1    1          4.6         3.1          1.5  4
5.1    1          5.0         3.6          1.4  5
6.1    1          5.4         3.9          1.7  6
1.2    2          5.1         3.5          1.4  1
2.2    2          4.9         3.0          1.4  2
3.2    2          4.7         3.2          1.3  3
4.2    2          4.6         3.1          1.5  4
5.2    2          5.0         3.6          1.4  5
6.2    2          5.4         3.9          1.7  6
Onyambu
  • 67,392
  • 3
  • 24
  • 53
0

If you rename the columns to match first you can use the rbind function to paste the last three columns below the first three columns.

names(df) <- rep(names(df)[1:3], 2)
df <- rbind(df[,1:3], df[,4:6])
Dave Ross
  • 673
  • 4
  • 12