Question

I would like to subset columns in a data frame in R based in the first row number into 2 dataframes (a and b)

My input dat:

    NE001457   NE001458   NE001494   NE001532   NE001533   NE001542
        1          0          1          1          1          1
 0.22951556  0.39403959  0.11835655  0.40844591 0.28442078 0.24236144
 0.04566349  0.14491037  0.03184582  0.0819644  0.18298301 -0.0339111
 0.04543686  0.05876655  0.23562874 -0.0717529 -0.0320517  -0.0851137

My expected output a:

     NE001457     NE001494   NE001532   NE001533   NE001542
        1             1          1          1          1
  0.22951556    0.11835655  0.40844591 0.28442078 0.24236144
  0.04566349    0.03184582  0.0819644  0.18298301 -0.0339111
  0.04543686   0.23562874  -0.0717529 -0.0320517  -0.0851137

My expected output b:

   NE001494 
       0          
  0.39403959  
  0.14491037  
  0.05876655  

I already have tried some thing like that a <- subset(dat, dat[1,] == 1) and b <- subset(dat, dat[1,] == 0) but not works.

Some ideas?

Cheers!

Was it helpful?

Solution

You can just select the columns with, for instance, a<-dat[,(dat[1,]) == 1]; the only trick is re-setting the column names when you end up extracting a single column.

datatxt <- "
NE001457   NE001458   NE001494   NE001532   NE001533   NE001542
 1          0          1          1          1          1
0.22951556  0.39403959  0.11835655  0.40844591 0.28442078 0.24236144 
0.04566349  0.14491037  0.03184582  0.0819644  0.18298301 -0.0339111
0.04543686  0.05876655  0.23562874 -0.0717529 -0.0320517  -0.0851137"

df <- read.table(text=datatxt,header=TRUE)

df
#    NE001457   NE001458   NE001494   NE001532   NE001533   NE001542
#1 1.00000000 0.00000000 1.00000000  1.0000000  1.0000000  1.0000000
#2 0.22951556 0.39403959 0.11835655  0.4084459  0.2844208  0.2423614
#3 0.04566349 0.14491037 0.03184582  0.0819644  0.1829830 -0.0339111
#4 0.04543686 0.05876655 0.23562874 -0.0717529 -0.0320517 -0.0851137

acols <- (df[1,] == 1)
bcols <- !acols

acols
#  NE001457 NE001458 NE001494 NE001532 NE001533 NE001542
#1     TRUE    FALSE     TRUE     TRUE     TRUE     TRUE

bcols
#  NE001457 NE001458 NE001494 NE001532 NE001533 NE001542
#1    FALSE     TRUE    FALSE    FALSE    FALSE    FALSE

a <- as.data.frame(df[,acols])
colnames(a) <- colnames(df)[acols]
b <- as.data.frame(df[,bcols])
colnames(b) <- colnames(df)[bcols]

a
#    NE001457   NE001494   NE001532   NE001533   NE001542
#1 1.00000000 1.00000000  1.0000000  1.0000000  1.0000000
#2 0.22951556 0.11835655  0.4084459  0.2844208  0.2423614
#3 0.04566349 0.03184582  0.0819644  0.1829830 -0.0339111
#4 0.04543686 0.23562874 -0.0717529 -0.0320517 -0.0851137

b
#    NE001458
#1 0.00000000
#2 0.39403959
#3 0.14491037
#4 0.05876655
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top