[R] Process multiple columns of data.frame

Thompson, David (MNR) David.John.Thompson at ontario.ca
Tue Nov 20 22:07:54 CET 2007


Jim,

What I'm getting at is my lack of understanding when manipulating
data frame indices. I have tried something like your matrix example
with variants of: Bout[ is.na(Bout[,c('bd.n','ht.n','dbh.n')]), ] <- 0

only to receive the following error message:
Error in `[<-.data.frame`(`*tmp*`, is.na(Bout[, c("bd.n", "ht.n",
"dbh.n")]),  : 
        non-existent rows not allowed

My questions were not focussed on the assignment value but,
on the selection of multiple columns (of a single type) to process.
Which is what I meant by concisely.

And, lo and behold, in my attempts to reframe my questions I (finally)
came upon a solution:
Bout[is.na(Bout$bd.n) | is.na(Bout$ht.n) | is.na(Bout$dbh.n),
c('bd.n','ht.n','dbh.n')] <- 0

Thank you for your time, DaveT.
*************************************
>-----Original Message-----
>From: jim holtman [mailto:jholtman at gmail.com] 
>Sent: November 20, 2007 02:25 PM
>To: Thompson, David (MNR)
>Cc: r-help at r-project.org
>Subject: Re: [R] Process multiple columns of data.frame
>
>It really depends on what you want to set the values that contain NAs
>to for the various type of the columns.  Do you always want numerics
>=0, characters ="", and factors =whatever?  Do you want to do this for
>all the columns in a dataframe?  If you want to it for all the columns
>in a matrix, it is easy, since all are the same type, and you are
>setting them to zero (e.g., yourMatrix[is.na(yourMatrix)] <- 0).
>
>You can always write a function that can take in the names of the
>columns and then depending on their types set the corresponding
>values.  So it depends on what you mean by "concisely" and how often
>you want to do it.
>
>On Nov 20, 2007 12:27 PM, Thompson, David (MNR)
><David.John.Thompson at ontario.ca> wrote:
>> Hello,
>>
>> How do I do the following more concisely?
>>        Bout[is.na(Bout$bd.n), 'bd.n'] <- 0
>>        Bout[is.na(Bout$ht.n), 'ht.n'] <- 0
>>        Bout[is.na(Bout$dbh.n), 'dbh.n'] <- 0
>>
>> Would the form of such a command be different
>> between numeric, character and factor columns?
>>
>> . . . between data.frames and matrices?
>>
>> Thanx, DaveT.
>> *************************************
>> Silviculture Data Analyst
>> Ontario Forest Research Institute
>> Ontario Ministry of Natural Resources
>> david.john.thompson at ontario.ca
>> http://ofri.mnr.gov.on.ca
>>
>> ______________________________________________
>> 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.
>>
>
>
>
>-- 
>Jim Holtman
>Cincinnati, OH
>+1 513 646 9390
>
>What is the problem you are trying to solve?
>



More information about the R-help mailing list