[R] Dinamic variables in loop

MacQueen, Don m@cqueen1 @end|ng |rom ||n|@gov
Wed May 9 17:29:38 CEST 2018


In addition to which, the original question uses an incorrect way to reference columns in the data frame.

It should probably have been:

newMyData <-MyData[!is.na(MyData$col1) | !is.na(MyData$col2) | !is.na(MyData$col3) | 
!is.na(MyData$col4) | !is.na(MyData$col5) , ]

That is assuming that "col1", "col2", etc. are actually the column names. Otherwise, maybe this

newMyData <-MyData[!is.na(MyData[,1]) | !is.na(MyData[,2] | !is.na(MyData[,3]) | 
!is.na(MyData[,4]) | !is.na(MyData[,5]) , ]

Also, I don't know what you mean when you say you want it to "write in for loop". Do you want the new data frame printed at each iteration? In that case, try putting
  print(newMyData)
inside the loop.

-Don

--
Don MacQueen
Lawrence Livermore National Laboratory
7000 East Ave., L-627
Livermore, CA 94550
925-423-1062
Lab cell 925-724-7509
 
 

On 5/8/18, 5:25 AM, "R-help on behalf of Thierry Onkelinx" <r-help-bounces using r-project.org on behalf of thierry.onkelinx using inbo.be> wrote:

    Dear Laura,
    
    It looks like you want to remove all rows for which each column is NA.
    You can to that with the code below.
    
    na.matrix <- is.na(MyData)
    all.na.row <- apply(na.matrix, 1, all)
    MyData[!all.na.row, ]
    
    Best regards,
    
    
    ir. Thierry Onkelinx
    Statisticus / Statistician
    
    Vlaamse Overheid / Government of Flanders
    INSTITUUT VOOR NATUUR- EN BOSONDERZOEK / RESEARCH INSTITUTE FOR NATURE
    AND FOREST
    Team Biometrie & Kwaliteitszorg / Team Biometrics & Quality Assurance
    thierry.onkelinx using inbo.be
    Havenlaan 88 bus 73, 1000 Brussel
    www.inbo.be
    
    ///////////////////////////////////////////////////////////////////////////////////////////
    To call in the statistician after the experiment is done may be no
    more than asking him to perform a post-mortem examination: he may be
    able to say what the experiment died of. ~ Sir Ronald Aylmer Fisher
    The plural of anecdote is not data. ~ Roger Brinner
    The combination of some data and an aching desire for an answer does
    not ensure that a reasonable answer can be extracted from a given body
    of data. ~ John Tukey
    ///////////////////////////////////////////////////////////////////////////////////////////
    
    
    
    
    2018-05-08 13:57 GMT+02:00 Laura Ringienė <lauraringiene using gmail.com>:
    > Hello,
    >
    > I am a newbie in R and I have a problem.
    > I want this line:
    >
    > newMyData <-MyData[!(is.na(col1)) | !(is.na(col2)) | !(is.na(col3)) | !(
    > is.na(col4)) | !(is.na(col5)),]
    >
    > write in for loop. The number of col[number] may be different from 3 to 8
    >
    > Is there any ideas?
    >
    > Thank you
    > Laura
    >
    >         [[alternative HTML version deleted]]
    >
    > ______________________________________________
    > R-help using r-project.org mailing list -- To UNSUBSCRIBE and more, see
    > 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.
    
    ______________________________________________
    R-help using r-project.org mailing list -- To UNSUBSCRIBE and more, see
    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