[R] Process multiple columns of data.frame

jim holtman jholtman at gmail.com
Tue Nov 20 20:24:40 CET 2007


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