Dear Members I have a data frame as generated below. I like to be able to call a function both with a vector and a vector (mydata$v1) in that data frame (v1). The first call works, but the second does not. Can someone help me with the second call? Thanks!! --- mydata<-data.frame(matrix(1:20,ncol=2)) colnames(mydata) <-c("v1","v2") summary(mydata) aaa<-function(data,w=w){ if(is.vector(w)){ out<-mean(w) } else { out<-mean(data[wt]) } return(out) } aaa(mydata,mydata$v1) aaa(mydata,"v1") # want this call to work --- Printout with error message> mydata<-data.frame(matrix(1:20,ncol=2)) > colnames(mydata) <-c("v1","v2") > summary(mydata)v1 v2 Min. : 1.00 Min. :11.00 1st Qu.: 3.25 1st Qu.:13.25 Median : 5.50 Median :15.50 Mean : 5.50 Mean :15.50 3rd Qu.: 7.75 3rd Qu.:17.75 Max. :10.00 Max. :20.00> > aaa<-function(data,w=w){+ if(is.vector(w)){ + out<-mean(w) + } else { + out<-mean(data[wt]) + } + return(out) + }> aaa(mydata,mydata$v1)[1] 5.5> aaa(mydata,"v1")[1] NA Warning message: In mean.default(w) : argument is not numeric or logical: returning NA>
You can do something like: aaa <- function(data, w=w) { if (class(w) %in% c("integer", "numeric", "double")) { out <- mean(w) } else { out <- mean(data[, w]) } return(out) } (there are some typos in your function you may want to double check, too) On Tue, Jun 23, 2015 at 5:39 PM, Steven Yen <syen04 at gmail.com> wrote:> mydata<-data.frame(matrix(1:20,ncol=2)) > colnames(mydata) <-c("v1","v2") > summary(mydata) > > aaa<-function(data,w=w){ > if(is.vector(w)){ > out<-mean(w) > } else { > out<-mean(data[wt]) > } > return(out) > } > > aaa(mydata,mydata$v1) > aaa(mydata,"v1") # want this call to work
Thanks! From this I learn the much needed class statement if (class(wt)=="character") wt <- x[, wt] which serves my need in a bigger project. Steven Yen On 6/23/2015 6:20 PM, boB Rudis wrote:> You can do something like: > > aaa <- function(data, w=w) { > if (class(w) %in% c("integer", "numeric", "double")) { > out <- mean(w) > } else { > out <- mean(data[, w]) > } > return(out) > } > > (there are some typos in your function you may want to double check, too) > > On Tue, Jun 23, 2015 at 5:39 PM, Steven Yen <syen04 at gmail.com> wrote: >> mydata<-data.frame(matrix(1:20,ncol=2)) >> colnames(mydata) <-c("v1","v2") >> summary(mydata) >> >> aaa<-function(data,w=w){ >> if(is.vector(w)){ >> out<-mean(w) >> } else { >> out<-mean(data[wt]) >> } >> return(out) >> } >> >> aaa(mydata,mydata$v1) >> aaa(mydata,"v1") # want this call to work >-- Steven Yen My e-mail alert: https://youtu.be/9UwEAruhyhY?list=PLpwR3gb9OGHP1BzgVuO9iIDdogVOijCtO
?with e.g. with(mydata,aaa(v1)) -- Bert Bert Gunter "Data is not information. Information is not knowledge. And knowledge is certainly not wisdom." -- Clifford Stoll On Tue, Jun 23, 2015 at 2:39 PM, Steven Yen <syen04 at gmail.com> wrote:> Dear Members > I have a data frame as generated below. I like to be able to call a > function both with a vector and a vector (mydata$v1) in that data frame > (v1). The first call works, but the second does not. Can someone help me > with the second call? Thanks!! > > --- > mydata<-data.frame(matrix(1:20,ncol=2)) > colnames(mydata) <-c("v1","v2") > summary(mydata) > > aaa<-function(data,w=w){ > if(is.vector(w)){ > out<-mean(w) > } else { > out<-mean(data[wt]) > } > return(out) > } > > aaa(mydata,mydata$v1) > aaa(mydata,"v1") # want this call to work > > --- > Printout with error message > >> mydata<-data.frame(matrix(1:20,ncol=2)) >> colnames(mydata) <-c("v1","v2") >> summary(mydata) > > v1 v2 > Min. : 1.00 Min. :11.00 > 1st Qu.: 3.25 1st Qu.:13.25 > Median : 5.50 Median :15.50 > Mean : 5.50 Mean :15.50 > 3rd Qu.: 7.75 3rd Qu.:17.75 > Max. :10.00 Max. :20.00 >> >> >> aaa<-function(data,w=w){ > > + if(is.vector(w)){ > + out<-mean(w) > + } else { > + out<-mean(data[wt]) > + } > + return(out) > + } >> >> aaa(mydata,mydata$v1) > > [1] 5.5 >> >> aaa(mydata,"v1") > > [1] NA > Warning message: > In mean.default(w) : argument is not numeric or logical: returning NA >> >> > > ______________________________________________ > R-help at 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.
On Jun 23, 2015, at 3:20 PM, boB Rudis wrote:> You can do something like: > > aaa <- function(data, w=w) { > if (class(w) %in% c("integer", "numeric", "double")) {I think you will find that inherits(w, "numeric") is more compact and safer. Both "integer" and "double" do inherit from "numeric" (and "double" is equivalent to "numeric") The test for 'is.vector' is also going to produce some surprises. List-objects may pass that test:> sum( list(1,2,3))Error in sum(list(1, 2, 3)) : invalid 'type' (list) of argument> is.vector( list(1,2,3))[1] TRUE> x=1:4 > attr(x, "some_attr") <- "something" > is.vector(x)[1] FALSE -- David.> out <- mean(w) > } else { > out <- mean(data[, w]) > } > return(out) > } > > (there are some typos in your function you may want to double check, too) > > On Tue, Jun 23, 2015 at 5:39 PM, Steven Yen <syen04 at gmail.com> wrote: >> mydata<-data.frame(matrix(1:20,ncol=2)) >> colnames(mydata) <-c("v1","v2") >> summary(mydata) >> >> aaa<-function(data,w=w){ >> if(is.vector(w)){ >> out<-mean(w) >> } else { >> out<-mean(data[wt]) >> } >> return(out) >> } >> >> aaa(mydata,mydata$v1) >> aaa(mydata,"v1") # want this call to work > > ______________________________________________ > R-help at 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.David Winsemius Alameda, CA, USA
How do I judge if a matrix contain any NA or otherwise non-missing, non-numerical? In the following, I would like to deliver ONE logical of TRUE or FALSE, rather than a 4 x 4 matrix containing TRUE or FALSE. Thank you. > a<-matrix(1:16,nrow=4) > diag(a)<-NA > a [,1] [,2] [,3] [,4] [1,] NA 5 9 13 [2,] 2 NA 10 14 [3,] 3 7 NA 15 [4,] 4 8 12 NA > is.na(a) [,1] [,2] [,3] [,4] [1,] TRUE FALSE FALSE FALSE [2,] FALSE TRUE FALSE FALSE [3,] FALSE FALSE TRUE FALSE [4,] FALSE FALSE FALSE TRUE