[R] R: re: dataframe

arun smartpink111 at yahoo.com
Wed Apr 30 15:49:33 CEST 2014




Hi,


In this case, you may have to use:
 dat1[sapply(dat1,function(x) any(grepl("\\bC\\b",x))) & sapply(dat1,function(x) any(grepl("Au5",x)))]
     o066_010    o066_022    o066_029    o066_034    o066_068    o066_072
1      sample      sample      sample      sample      sample      sample
2         Au5         Au5         Au5         Au5         Au5         Au5
3           C           C           C           C           C           C
4 14536204.77 13846871.69 13140449.17 14979396.48 11533763.33 12506876.82
     o066_079    o066_081    o066_086
1      sample      sample      sample
2         Au5         Au5         Au5
3           C           C           C
4 11310021.96 10716085.25 9956435.681


## using all the combinations
indxNew <- read.table(text = unique(apply(dat1[2:3, grep("^o", colnames(dat1))], 
    2, paste, collapse = " ")), sep = "", header = FALSE, stringsAsFactors = FALSE)

lapply(seq_len(nrow(indxNew)), function(i) {
    indx1 <- which(sapply(dat1, function(x) any(grepl(indxNew[i, 1], x))) & sapply(dat1, 
        function(x) any(grepl(paste0("\\b", indxNew[i, 2], "\\b"), x))))
    dat1[indx1]
})



#You can add the other columns in the dataset as I showed in the previous email.


A.K.



On Wednesday, April 30, 2014 8:57 AM, "pascalbells at libero.it" <pascalbells at libero.it> wrote:

I've tried this one and it works:


df[sapply(df, function(x) any(grepl("Au5", x)))]



but How to write also that I want "C" too?

regards,

james



>----Messaggio originale----
>Da: smartpink111 at yahoo.com
>Data: 30/04/2014 11.02
>A: "pascalbells at libero.it"<pascalbells at libero.it>
>Ogg: re: [R] dataframe
>
>Could you dput the dataset?
>Also,the expected result...
>
>----------
>Sent from my Nokia
>
>------Original message------
>From: pascalbells at libero.it <pascalbells at libero.it>
>To: <smartpink111 at yahoo.com>
>Date: Wednesday, April 30, 2014 9:40:06 AM GMT+0200
>Subject: R: Re: [R] dataframe
>
>thank you for your help but it doesn't work....
>
>James
>
>
>
>>----Messaggio originale----
>>Da: smartpink111 at yahoo.com
>>Data: 29/04/2014 17.59
>>A: "r-help at r-project.org"<r-help at r-project.org>
>>Cc: "pascalbells at libero.it"<pascalbells at libero.it>
>>Ogg: Re: [R] dataframe
>>
>>Hi,
>>
>>It is better to show example data using ?dput().
>>
>>dat <- structure(list(row.names = 1:4, XYZ = c("sample", "sample2", 
>>"sample3", "sample4"), `000_001` = c("sample", "Au5", "C", "C"
>>), `000_002` = c("sample", "Au32", "C", "Au4"), `000_003` = c("sample", 
>>"Au5", "A", "AC")), .Names = c("row.names", "XYZ", "000_001", 
>>"000_002", "000_003"), class = "data.frame", row.names = c(NA, 
>>-4L))
>>
>>
>>dat[,intersect(grep("Au5", dat), grep("\\bC\\b",dat)),drop=FALSE]
>>#  000_001
>>#1  sample
>>#2     Au5
>>#3       C
>>#4       C
>>
>>A.K.
>>
>>
>>
>>
>>On Tuesday, April 29, 2014 9:45 AM, "pascalbells at libero.it" 
><pascalbells at libero.it> wrote:
>>Hello, 
>>thank you for accepting me into the list.
>>
>>I have the following dataframe:
>>
>>   row.names  X           Y       Z     000_001  000_002  000_003
>>1                    sample                     sample    sample     sample
>>2                    sample2                   Au5         Au32       Au5
>>3                    sample3                   C              C             
C
>>4                    ..........  
>>..
>>..

>n                    ...........                    ....            .......         
>............
>>
>>
>>I would like to select al the columns that have Au5 and C.
>>thank you in advance for your help,
>>james
>>
>>
>>    [[alternative HTML version deleted]]
>>
>>______________________________________________
>>R-help at r-project.org mailing list
>>https://stat.ethz.ch/mailman/listinfo/r-help
>>PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
>>and provide commented, minimal, self-contained, reproducible code.
>>
>>
>
>
>
>



More information about the R-help mailing list