Dear R-community,
my data set looks like 'mat' below.
Plant<-c(NA,1,1,1,NA,NA,NA,NA,NA,1);
Value1<-rnorm(1:10);
Value2<-rnorm(1:10);
mat<-cbind(Plant,Value1,Value2);
I receive data from two different sites.
One site is identified by an interger number, the other site has no data in
column Plant=NA.
My pb:
I'm trying to assign labels "A" or "B" to these 2 sites
into a new column,
but my if(){} else{} statement fails with the following statement:
Error in if (is.na(mat$Plant == TRUE)) { :
argument is of length zero
if(is.na(mat$Plant==TRUE)){mat$Plant1="A"}
else{mat$Plant1="B"};
I looked through the avail doc and R-help for some time but wasn't able to
fix the pb.
Thx Hans
Try this version:
Plant<-c(NA,1,1,1,NA,NA,NA,NA,NA,1);
Value1<-rnorm(10);
Value2<-rnorm(10);
mat<-data.frame(Plant,Value1,Value2);
mat <- transform(mat, Plant1=ifelse(is.na(Plant),
"A","B"))
A couple of comments on your original code:
The 1:10 in the rnorm calls is unnecessary since rnorm just takes the
length of the vector, just tell rnorm how many numbers you want (or you
could do rnorm(Plant) to have R count for you).
The cbind function creates a matrix, the notation mat$Plant assumes that
mat is a list or data frame and that is why you received the error.
The if (){} else {} syntax is usually for program flow and looks for a
scalar value, the function ifelse is vectorized and usefull for vectors
(which is what it looks like you wanted).
The transform function is nice in that it allows you to skip some of the
indexing.
Hope this helps,
--
Gregory (Greg) L. Snow Ph.D.
Statistical Data Center
Intermountain Healthcare
greg.snow at intermountainmail.org
(801) 408-8111
-----Original Message-----
From: r-help-bounces at stat.math.ethz.ch
[mailto:r-help-bounces at stat.math.ethz.ch] On Behalf Of Hans-Juergen
Eickelmann
Sent: Tuesday, December 05, 2006 8:33 AM
To: r-help at stat.math.ethz.ch
Subject: [R] if(){} else{}
Dear R-community,
my data set looks like 'mat' below.
Plant<-c(NA,1,1,1,NA,NA,NA,NA,NA,1);
Value1<-rnorm(1:10);
Value2<-rnorm(1:10);
mat<-cbind(Plant,Value1,Value2);
I receive data from two different sites.
One site is identified by an interger number, the other site has no data
in column Plant=NA.
My pb:
I'm trying to assign labels "A" or "B" to these 2 sites
into a new
column, but my if(){} else{} statement fails with the following
statement:
Error in if (is.na(mat$Plant == TRUE)) { :
argument is of length zero
if(is.na(mat$Plant==TRUE)){mat$Plant1="A"}
else{mat$Plant1="B"};
I looked through the avail doc and R-help for some time but wasn't able
to fix the pb.
Thx Hans
______________________________________________
R-help at stat.math.ethz.ch 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.
Test should be
if(is.na(mat$Plant)){ ...
On Tue, 5 Dec 2006, Hans-Juergen Eickelmann wrote:
>
> Dear R-community,
>
> my data set looks like 'mat' below.
>
> Plant<-c(NA,1,1,1,NA,NA,NA,NA,NA,1);
> Value1<-rnorm(1:10);
> Value2<-rnorm(1:10);
> mat<-cbind(Plant,Value1,Value2);
> I receive data from two different sites.
> One site is identified by an interger number, the other site has no data in
> column Plant=NA.
>
> My pb:
>
> I'm trying to assign labels "A" or "B" to these 2
sites into a new column,
> but my if(){} else{} statement fails with the following statement:
> Error in if (is.na(mat$Plant == TRUE)) { :
> argument is of length zero
>
> if(is.na(mat$Plant==TRUE)){mat$Plant1="A"}
else{mat$Plant1="B"};
>
> I looked through the avail doc and R-help for some time but wasn't able
to
> fix the pb.
>
> Thx Hans
>
> ______________________________________________
> R-help at stat.math.ethz.ch 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.
>
_________________________________________________________________
David Scott Visiting (Until January 07)
Department of Probability and Statistics
The University of Sheffield
The Hicks Building
Hounsfield Road
Sheffield S3 7RH
United Kingdom
Phone: +44 114 222 3908
Email: d.scott at auckland.ac.nz
On Tue, 2006-12-05 at 16:33 +0100, Hans-Juergen Eickelmann wrote:> Dear R-community, > > my data set looks like 'mat' below. > > Plant<-c(NA,1,1,1,NA,NA,NA,NA,NA,1); > Value1<-rnorm(1:10);You only really need rnorm(10), as in ?rnorm, rnorm is defined as rnorm(n, mean=0, sd=1) and n is the number of observations.> Value2<-rnorm(1:10); > mat<-cbind(Plant,Value1,Value2);You don't need the ";" at the ends of the lines, and cbind() returns a matrix, for which you cannot use "$" to access the columns:> class(mat)[1] "matrix"> mat$PlantNULL What you are looking for is ifelse(), see ?ifelse, but here is your example, suitable spaced out and minus the other infelicities. Plant <- c(NA, 1, 1, 1, NA, NA, NA, NA, NA, 1) Value1 <- rnorm(10) Value2 <- rnorm(10) mat <- data.frame(Plant, Value1, Value2) mat$Plant1 <- ifelse(is.na(mat$Plant), "A", "B")> mat$Plant1[1] "A" "B" "B" "B" "A" "A" "A" "A" "A" "B"> matPlant Value1 Value2 Plant1 1 NA 2.76603270 -0.20435729 A 2 1 -0.54688170 -0.81943566 B 3 1 0.30480812 -0.05404563 B 4 1 1.64959026 -0.10762260 B 5 NA 1.13528236 -0.04670294 A 6 NA 1.55636761 0.87617575 A 7 NA 0.40651924 1.90516887 A 8 NA 1.49827147 0.05080935 A 9 NA -0.04396752 0.53267040 A 10 1 0.42714137 -0.55944595 B HTH G> I receive data from two different sites. > One site is identified by an interger number, the other site has no data in > column Plant=NA. > > My pb: > > I'm trying to assign labels "A" or "B" to these 2 sites into a new column, > but my if(){} else{} statement fails with the following statement: > Error in if (is.na(mat$Plant == TRUE)) { : > argument is of length zero > > if(is.na(mat$Plant==TRUE)){mat$Plant1="A"} else{mat$Plant1="B"};That's not how you use is.na(), see ?is.na, as is.na(x) returns TRUE/FLASE depending on wither x is NA or not> I looked through the avail doc and R-help for some time but wasn't able to > fix the pb. > > Thx Hans > > ______________________________________________ > R-help at stat.math.ethz.ch 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.-- %~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~% Gavin Simpson [t] +44 (0)20 7679 0522 ECRC & ENSIS, UCL Geography, [f] +44 (0)20 7679 0565 Pearson Building, [e] gavin.simpsonATNOSPAMucl.ac.uk Gower Street, London [w] http://www.ucl.ac.uk/~ucfagls/ UK. WC1E 6BT. [w] http://www.freshwaters.org.uk %~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%
Hi Hans, try this ... mat <- as.data.frame(cbind(Plant,Value1,Value2)) mat$Plant1 <- ifelse(is.na(mat$Plant), "A", "B") Cheers Andrew On Tue, Dec 05, 2006 at 04:33:15PM +0100, Hans-Juergen Eickelmann wrote:> > Dear R-community, > > my data set looks like 'mat' below. > > Plant<-c(NA,1,1,1,NA,NA,NA,NA,NA,1); > Value1<-rnorm(1:10); > Value2<-rnorm(1:10); > mat<-cbind(Plant,Value1,Value2); > I receive data from two different sites. > One site is identified by an interger number, the other site has no data in > column Plant=NA. > > My pb: > > I'm trying to assign labels "A" or "B" to these 2 sites into a new column, > but my if(){} else{} statement fails with the following statement: > Error in if (is.na(mat$Plant == TRUE)) { : > argument is of length zero > > if(is.na(mat$Plant==TRUE)){mat$Plant1="A"} else{mat$Plant1="B"}; > > I looked through the avail doc and R-help for some time but wasn't able to > fix the pb. > > Thx Hans > > ______________________________________________ > R-help at stat.math.ethz.ch 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.-- Andrew Robinson Department of Mathematics and Statistics Tel: +61-3-8344-9763 University of Melbourne, VIC 3010 Australia Fax: +61-3-8344-4599 http://www.ms.unimelb.edu.au/~andrewpr http://blogs.mbs.edu/fishing-in-the-bay/
Hi a little bit quicker solution is based on subsetting and a fact that logical vector can be treated as numeric with FALSE=0 and TRUE = 1 Plant<-sample(c(NA,1), 100000, replace=T); Value1<-rnorm(Plant); Value2<-rnorm(Plant); mat<-data.frame(Plant=Plant,Value1=Value1,Value2=Value2)> system.time(mat$Plant1<-c("B","A")[is.na(mat$Plant)+1])[1] 0.03 0.02 0.05 NA NA> system.time(mat$Plant2 <- ifelse(is.na(mat$Plant), "A", "B"))[1] 0.28 0.01 0.30 NA NA> with(mat, all.equal(Plant1, Plant2))[1] TRUE HTH Petr On 6 Dec 2006 at 6:39, Andrew Robinson wrote: Date sent: Wed, 6 Dec 2006 06:39:32 +1100 From: Andrew Robinson <A.Robinson at ms.unimelb.edu.au> To: Hans-Juergen Eickelmann <EICKELMA at de.ibm.com> Copies to: r-help at stat.math.ethz.ch Subject: Re: [R] if(){} else{}> Hi Hans, > > try this ... > > mat <- as.data.frame(cbind(Plant,Value1,Value2)) > > mat$Plant1 <- ifelse(is.na(mat$Plant), "A", "B") > > Cheers > > Andrew > > > On Tue, Dec 05, 2006 at 04:33:15PM +0100, Hans-Juergen Eickelmann > wrote: > > Dear R-community, > > my data set looks like 'mat' below. > > > Plant<-c(NA,1,1,1,NA,NA,NA,NA,NA,1); > Value1<-rnorm(1:10); > > Value2<-rnorm(1:10); > mat<-cbind(Plant,Value1,Value2); > I receive > data from two different sites. > One site is identified by an interger > number, the other site has no data in > column Plant=NA. > > My pb: > > > I'm trying to assign labels "A" or "B" to these 2 sites into a new > column, > but my if(){} else{} statement fails with the following > statement: > Error in if (is.na(mat$Plant == TRUE)) { : > > argument is of length zero > > > if(is.na(mat$Plant==TRUE)){mat$Plant1="A"} else{mat$Plant1="B"}; > > I > looked through the avail doc and R-help for some time but wasn't able > to > fix the pb. > > Thx Hans > > > ______________________________________________ > > R-help at stat.math.ethz.ch 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. > > -- > Andrew Robinson > Department of Mathematics and Statistics Tel: > +61-3-8344-9763 University of Melbourne, VIC 3010 Australia > Fax: +61-3-8344-4599 http://www.ms.unimelb.edu.au/~andrewpr > http://blogs.mbs.edu/fishing-in-the-bay/ > > ______________________________________________ > R-help at stat.math.ethz.ch 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.Petr Pikal petr.pikal at precheza.cz