[R] Why is my data always imported as a list?
S Ellison
S.Ellison at LGCGroup.com
Tue Jun 12 11:25:54 CEST 2012
Noone has yet mentioned tjhe 'standard' way to reference a data frame item, which uses $
boxplot(CB_un[["Value]]~CB_un[["State.Fips"]]) #Note the double [[ ]]
# is equivalent to
boxplot(CB_un$Value~CB_un$State.Fips)
# and can be achieved by
boxplot(Value~State.Fips, data=CB_un)
The rather awkward [["name"]] notation is usually only needed if the name does not comply with variable naming requirements (including list and data frame names); for example, names containing spaces or operator symbols (like "+") are not allowed for ordinary variables. It is possible and sometimes useful to create non-standard data frame names for display; for example, anova.lm actually returns a data frame with names "Df", "Sum Sq", "Mean Sq", F value" and "Pr(>F)". But it makes manipulation a tad trickier and you'd be unable to use them in the context of a formula with a data argument.
S Ellison
> -----Original Message-----
> From: r-help-bounces at r-project.org
> [mailto:r-help-bounces at r-project.org] On Behalf Of Samantha Sifleet
> Sent: 11 June 2012 17:29
> To: r-help at r-project.org
> Subject: [R] Why is my data always imported as a list?
>
> Hi,
>
> I am a relatively new to R. So, this is probably a really
> basic issue that I keep hitting.
>
> I read my data into R using the read.csv command:
>
> x = rep("numeric", 3)
> CB_un=read.csv("Corn_Belt_unirr.csv", header=TRUE,
> colClasses=c("factor",
> x))
>
> # I have clearly told R that I have one factor variable and 3
> numeric variables in this table.
> #but if I try to do anything with them, I get an error
>
> boxplot(CB_un["Value"]~CB_un["State.Fips"])
>
> Error in model.frame.default(formula = CB_un["Value"] ~
> CB_un["State.Fips"]) :
> invalid type (list) for variable 'CB_un["Value"]'
>
> # Because these variables are all stored as lists.
> #So, I have to unpack them.
>
> CB_unirr_rent<-as.numeric(unlist(CB_un["Value"]))
> CB_unirr_State<-as.factor(unlist(CB_un["State.Fips"]))
>
> #before I can do anything with them
>
> boxplot(CB_unirr_rent~CB_unirr_State)
>
> Is there a reason my data is always imported as lists? Is
> there a way to skip this upacking step?
>
> Thanks,
>
> Sam
> [[alternative HTML version deleted]]
>
> ______________________________________________
> 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.
> *******************************************************************
This email and any attachments are confidential. Any use...{{dropped:8}}
More information about the R-help
mailing list