arrow, business, financial

The best way to Stack Information Body Columns in R

Posted on
banner 336x280

Ceaselessly chances are you’ll need to stack two or extra information body columns into one column in R.

As an example, chances are you’ll need to move from this:

banner 468x60
  individual trial outcome1 outcome2
     A     1        7        4
     A     2        6        4
     B     1        6        5
     B     2        5        5
     C     1        4        3
     C     2        4        2

To this:

   individual trial results  price
      A     1   outcome1     7
      A     2   outcome1     6
      B     1   outcome1     6
      B     2   outcome1     5
      C     1   outcome1     4
      C     2   outcome1     4
      A     1   outcome2     4
      A     2   outcome2     4
      B     1   outcome2     5
      B     2   outcome2     5
      C     1   outcome2     3
      C     2   outcome2     2

This instructional explains two modes you’ll be able to significance in R to try this.

Form 1: Utility the Stack Serve as in Bottom R

Refer to code presentations the right way to stack columns the use of the stack serve as in bottom R:

#manufacture unedited information body
information <- information.body(individual=c('A', 'A', 'B', 'B', 'C', 'C'),
                   trial=c(1, 2, 1, 2, 1, 2),
                   outcome1=c(7, 6, 6, 5, 4, 4),
                   outcome2=c(4, 4, 5, 5, 3, 2))

#stack the 3rd and fourth columns
cbind(information[1:2], stack(information[3:4]))

   individual trial values      ind
1       A     1      7 outcome1
2       A     2      6 outcome1
3       B     1      6 outcome1
4       B     2      5 outcome1
5       C     1      4 outcome1
6       C     2      4 outcome1
7       A     1      4 outcome2
8       A     2      4 outcome2
9       B     1      5 outcome2
10      B     2      5 outcome2
11      C     1      3 outcome2
12      C     2      2 outcome2

Form 2: Utility the Soften Serve as from Reshape2

Refer to code presentations the right way to stack columns the use of the soften serve as from the reshape2 library:

#load library
library(reshape2)

#manufacture unedited information body
information <- information.body(individual=c('A', 'A', 'B', 'B', 'C', 'C'),
                   trial=c(1, 2, 1, 2, 1, 2),
                   outcome1=c(7, 6, 6, 5, 4, 4),
                   outcome2=c(4, 4, 5, 5, 3, 2))

#soften columns of information body
soften(information, identification.var = c('individual', 'trial'), variable.identify="results")

   individual trial results price
1       A     1 outcome1     7
2       A     2 outcome1     6
3       B     1 outcome1     6
4       B     2 outcome1     5
5       C     1 outcome1     4
6       C     2 outcome1     4
7       A     1 outcome2     4
8       A     2 outcome2     4
9       B     1 outcome2     5
10      B     2 outcome2     5
11      C     1 outcome2     3
12      C     2 outcome2     2

You’ll in finding your entire documentation for the soften serve as right here.

Spare Assets

The best way to Transfer Two Columns in R
The best way to Rename Columns in R
The best way to Sum Particular Columns in R

banner 336x280

Leave a Reply

Your email address will not be published. Required fields are marked *