Search code examples
arraysrdata-manipulation

array manipulations and extractions of data by names


I need to manipulate my array. I have an array call res which contain all my results. See just below the input.

    RT   Max    blk24  blk23    mat23   sm23    sm24
1   1.6  261    13     19       19      27      20
2   1.6  284    NA     53       5       99      91
3   1.7  304    NA     NA       NA      81      NA
4   1.8  305    32     28       28      39      33
5   1.9  322    8      NA       NA      36      39
6   1.9  349    NA     NA       NA      24      17
7   2    389    1       3       NA      50      NA
8   2.1  393    80      70      7       12      71
9   2.1  622    NA      NA      15      97      96
10  2.2  705    NA      NA      NA      NA      32

I want to extract my data according to the column name which are here blk, and sm and a criteria of the value by itself.

For blk I propose to do like that:

whereblk<- grep("^Blk", colnames(res))

for (i in 1:length(res)){
    tmp.res<-res[which(res[whereblk]>0),]
}

I get an output which give me all the RT and Max value when blk>0.

then I would like to extract the values from sm columns. I want to extract sm values when blk=NA and mat=NA.

do you have any clues?

My desired output is:

    RT  Max         sm23    sm24
1   
2   
3   1.7 304         81      NA
4   
5   
6   1.9 349         24      17
7   
8   
9   
10  2.2 705         NA      32

To do it using excel, it is possible to use the followed formula, =IF(COUNTIF(blk:blk,">0"),"-",sm). The problem with excel is, I cannot use it automatically if for example the number of blk column changes.

The difficulty here is to use formula which localizes the column position according to its name. And extract the value according to two criteria which is something I still have not mastered.

If you have any suggestion, you are very welcome.


Solution

  • Here is a dplyr solution.

    I am fairly sure @thelatemail provided a solution that will work using just base R in the comments section.

    library(dplyr)
    
    df <- read.table(text = "RT   Max    blk24  blk23    mat23   sm23    sm24
        1   1.6  261    13     19       19      27      20
        2   1.6  284    NA     53       5       99      91
        3   1.7  304    NA     NA       NA      81      NA
        4   1.8  305    32     28       28      39      33
        5   1.9  322    8      NA       NA      36      39
        6   1.9  349    NA     NA       NA      24      17
        7   2    389    1       3       NA      50      NA
        8   2.1  393    80      70      7       12      71
        9   2.1  622    NA      NA      15      97      96
        10  2.2  705    NA      NA      NA      NA      32", header = T)
    
    nams <- df %>% select(matches('blk|mat')) 
    df <- df[apply(nams, 1, function(i) all(is.na(i)))==TRUE,]
    
    df
    
        RT Max blk24 blk23 mat23 sm23 sm24
    3  1.7 304    NA    NA    NA   81   NA
    6  1.9 349    NA    NA    NA   24   17
    10 2.2 705    NA    NA    NA   NA   32