I think this should be relatively simple. I am using the latest release of R. In a data frame, I have a column with ID numbers called PairID and a column called species with 15 different species. I want to know which PairID numbers have all 15 species.
The data frame looks something like
head(analysis.df)
species PairID
DIKDIK 1
GAZELLE 2
GIRAFFE 1
ELAND 5
GIRAFFE 3
DIKDIK 2
my idea was to run this:
for(i in 1:nrow(analysis.df)) {
if (analysis.df$species[i]=="GRANTS GAZELLE") {analysis.df$GRANTS GAZELLE[i] <- 1}
else if (analysis.df$species[i]=="DIKDIK") {analysis.df$DIKDIK[i] <- 1
else if (analysis.df$species[i]=="IMPALA") {analysis.df$IMPALA[i] <- 1}
else if (analysis.df$species[i]=="BUFFALO") {analysis.df$BUFFALO[i] <- 1}
else if (analysis.df$species[i]=="BUSHBUCK") {analysis.df$BUSHBUCK[i] <- 1}
else if (analysis.df$species[i]=="GIRAFFE") {analysis.df$GIRAFFE[i] <- 1}
else if (analysis.df$species[i]=="ELAND") {analysis.df$ELAND[i] <- 1}
else if (analysis.df$species[i]=="GERENUK") {analysis.df$GERENUK[i] <- 1}
else if (analysis.df$species[i]=="LESSER KUDU") {analysis.df$LESSER KUDU[i] <- 1}
else if (analysis.df$species[i]=="HARTEBEEST") {analysis.df$HARTEBEEST[i] <- 1}
else if (analysis.df$species[i]=="STEENBOK") {analysis.df$STEENBOK[i] <- 1}
else if (analysis.df$species[i]=="ORYX") {analysis.df$ORYX[i] <- 1}
else if (analysis.df$species[i]=="REEDBUCK") {analysis.df$REEDBUCK[i] <- 1}
else if (analysis.df$species[i]=="THOMSONS GAZELLE") {analysis.df$THOMSONS GAZELLE[i] <- 1}
else if (analysis.df$species[i]=="WATERBUCK") {analysis.df$WATERBUCK[i] <- 1}
}
Then I could try summary
for all rows with a 1 in all of these newly created columns.
But this code gives the error:
> Error: unexpected symbol in:
"for(i in 1:nrow(analysis.df)){
if (analysis.df$species[i]=="GRANTS GAZELLE") {analysis.df$GRANTS GAZELLE"
I have looked here and here plus some vignettes in R and google searches but haven't been able to crack it so far. I am not even sure this method would give me what I want and would happily take a look at any suggestions to achieve the goal initially stated at the beginning of this post.