[R] Splitting a dataframe by character vector
Rolf Turner
rolf.turner at xtra.co.nz
Sat Apr 21 06:16:53 CEST 2012
On 21/04/12 13:52, Ben Neal wrote:
> 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). I just want to generate summary statistics for my other variables for each site individually.
>
> I know this should be simple, and I did read up on options, choosing to use "subset", but I am honestly confounded that this does not result in a new dataframe split by this factor. I tried "subset" with an argument based on my various other numeric vectors, and it worked just fine. My very simple code is below, and thank you for any responses. I apologize for asking such a simple question, but I wrote this out exactly as found it in an exactly similar question, which indicated this should work. Now I am confused! Thank you for any assistance. Ben Neal
>
> # Load data from CSV file
> Cover = read.csv ("/Users/benjaminneal/Documents/1110_Panama/Transect series /1210_BocasTransectSummary.csv",
> header=T)
> # Divide dataframe by Site names
> Site1<- subset(Cover, Site = "PtaCaracol")
>
> PS The csv loads fine, gives normal summary stats, and does not seem to be the issue. I thought about just renaming by hand all the sites in the file (i.e. "PtaCaracol"=1 . . but this seems like a weak solution).
What about a ***reproducible*** example?
What isn't working? What (if any) error message did you get?
I am at least 99% confident that numeric versus character values in
Cover$Site
is completely irrelevant.
My ***guess*** (and it can only be a guess, given the vagueness and
lack of detail in your question) is that you need a double ("logical")
equals sign. I.e. I suspect that
Site1<- subset(Cover, Site == "PtaCaracol")
will work.
That being said --- since you want to *split* your data frame by "Site" ---
why the <expletive deleted> don't you use split()???
E.g.:
Splitz <- split(Cover,Cover$Site)
cheers,
Rolf Turner
More information about the R-help
mailing list