[R] Splitting a dataframe by character vector

Ben Neal bneal at spg.ucsd.edu
Mon Apr 23 17:55:48 CEST 2012


Thanks Steve, your explanation of the "==" is very helpful, and I will look up ?by. Ben 


-----Original Message-----
From: S Ellison [mailto:S.Ellison at LGCGroup.com]
Sent: Mon 4/23/2012 3:19 AM
To: Ben Neal; r-help at r-project.org
Subject: RE: Splitting a dataframe by character vector
 
 

> -----Original Message-----
> I am just trying to split a dataframe of 750 observations of 
> 29 variables by "Site", which is a vector in the dataframe 
> with five text names (ex. PtaCaracol). 
A couple of methods.
i) First, look up ?split, which chops your data frame into a list of five data frames. Then use lapply on the list to get a list of summaries, or sapply to get something that will (if the results of your summary are a simple number or vector) look like an array.

ii) look up ?by, which will do something like lapply (and returns a list with extra features) and will print a tidier result.

> I tried "subset" ....
> # Divide dataframe by Site names
> Site1 <- subset(Cover, Site = "PtaCaracol")

Check your syntax again; that should have been
 Site1 <- subset(Cover, Site == "PtaCaracol")

'=' is a pairwise link or an assignment operator, not the equality test that subset would be looking for. 

Steve E
*******************************************************************
This email and any attachments are confidential. Any use...{{dropped:8}}



More information about the R-help mailing list