[R] Why is my data always imported as a list?

Patrick Connolly p_connolly at slingshot.co.nz
Tue Jun 12 13:07:13 CEST 2012


On Mon, 11-Jun-2012 at 12:29PM -0400, Samantha Sifleet wrote:

|> 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"])

Others have given good suggestions, but a slight modification of your
code would work if your dataframe is what we'd like to think it is:

boxplot(CB_un[,"Value"]~CB_un[,"State.Fips"])

or

boxplot(CB_un[["Value"]]~CB_un[["State.Fips"]])

I can't check if those will work, but even if they do, the formula
with a data argument is more elegant.  

HTH




|> 
|> 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.

-- 
~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.   
   ___    Patrick Connolly   
 {~._.~}                   Great minds discuss ideas    
 _( Y )_  	         Average minds discuss events 
(:_~*~_:)                  Small minds discuss people  
 (_)-(_)  	                      ..... Eleanor Roosevelt
	  
~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.



More information about the R-help mailing list