[R] lists everywhere
William Dunlap
wdunlap at tibco.com
Mon Dec 5 20:50:59 CET 2011
First, some general suggestions:
To see the structure of an object I would recommend
the str() function or, for a more concise output,
the class() function.
I don't think most ordinary users should be using
is.list() and, especially, is.vector().
Now for the particulars.
dbGetQuery probably returns an object of class "data.frame".
data.frames are implemented as lists with certain attributes,
so if 'dat' is a data.frame then is.list(dat) reports TRUE.
class(dat) would report "data.frame", which is what you want
to know. Similarly, dat[1] is a data.frame so is.list(dat[1])
reports TRUE. dat[[1]] is a column of a data.frame, not a
data.frame, and class(dat[[1]]) will tell you its class.
I haven't come across a case where is.vector is useful.
is.vector(x) returns TRUE if x has no attributes other than "names"
is x is not a language object, environment, or other esoteric
type. E.g., is.vector of a factor object returns FALSE and
is.vector of a numeric object or a list object returns TRUE
(unless the object has some attributes).
is.vector has nothing to do with the concept of a vector in linear
algebra (or aviation or physics or almost anywhere else).
Bill Dunlap
Spotfire, TIBCO Software
wdunlap tibco.com
> -----Original Message-----
> From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On Behalf Of Kehl Dániel
> Sent: Monday, December 05, 2011 11:21 AM
> To: r-help at r-project.org
> Subject: [R] lists everywhere
>
> Dear list members,
>
> I have a really simple problem.
> I connected to a DB and have the following query
>
> adat <- dbGetQuery(con, paste("select * from kmdata where SzeAZ='",
> szeazok[i], "' order by datum", sep=""))
>
> now I have the data in the adat variable which is a list. In fact the
> elements of the list are lists as well
> > is.list(adat)
> [1] TRUE
> > is.list(adat[10])
> [1] TRUE
> > is.list(adat[[10]])
> [1] FALSE
> > is.vector(adat[[10]])
> [1] TRUE
>
> I simply want to use a function with sapply on the 13-76th columns of
> the original list.
> sapply(data[[13:76]], myfunc) does not work of course. I tried to define
> 13:76 in a vector, still no result.
> How is it possible?
>
> thanks a lot
> d
>
> ______________________________________________
> 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.
More information about the R-help
mailing list