Nick
Nick

Reputation: 1

Joining two columns (1 factor, 1 integer) in R

Below is the first 5 rows of my data: the data frame is called inverts

    Location Transect  Species         Count
    McAbee     M1       Bat Star         35
    McAbee     M1       Turban Snail     2
    McAbee     M1       Sun Star         1
    McAbee     M1       Chiton           1
    ..........

I am trying to join my species and count data together so I can perform an ANOVA to see the differences between location and transect. I have two locations and four transects total.

I believe the tapply() function is the correct one to use to join Species and Count together but I can't figure out the code.

I believe the code should be:

inverts$speciescount = tapply(inverts$Species, inverts$Count, ....)

So I have gotten some good feedback on how to combine the two columns, however, I am still unable to compare the data between transects and location. I am unsure on how to proceed. What I want to do is create a code where it is:

Count ~ Transect
# or 
Count ~ Location

The problem with just doing that is the Count data is just a bunch of numbers and it is referenced to a species. Does anyone have any suggestions?

Thanks for helping

Upvotes: 0

Views: 1226

Answers (3)

IRTFM
IRTFM

Reputation: 263342

I think you are confused about what your inputs to a modeling function should be. If you are modeling counts, then something like this will be needed:

cfit <- glm(counts ~  transect + location + species, data=inverts, family="poisson")
anova(cfit)

If you wanted to look at the interaction of species with location, then you might examine this model:

cfit2 <- glm(counts ~  transect + location + species, data=inverts, family="poisson")
anova(cfit2)

It would be possible to do a linear regression, but then you may get nonsensical predictions like negative counts.

Upvotes: 0

Hong Ooi
Hong Ooi

Reputation: 57686

You can do this either the character way:

within(inverts, speciesCount <- paste(Species, Count, sep=":")

or the factor way:

within(inverts, speciesCount <- Species:factor(Count))

Since this is in the context of linear modelling, the factor way seems more appropriate.

Upvotes: 1

diliop
diliop

Reputation: 9451

If I understand your question correctly, for d being your data.frame:

newd <- data.frame(d[,c("Location","Transect")],SpeciesCount=paste(d$Species,d$Count))

Upvotes: 0

Related Questions