Skip to content
Prev 2177 / 7420 Next

Multiple count if style "queries"

Hi Kay,

Thanks very much for this - the workaround works fine. The problem i had ( i think) was that the text formatting was odd - a lot of the eco_names had accents which got messed up ( sure there is a technical term!) when i extracted them from the GIS database into excel.

Thanks again to you and Eduard

Chris
On 23 May 2011, at 12:32, Kay Cecil Cichini wrote:
Hi,

for curiosity i tried your full dataset:
and cast() works well in my case!
you can also do this "by hand" - and will get the same result.

best,
kay

###
require(reshape)

dfm<- melt(data2, id = "ECO_NAME")
dfc<- cast(dfm, ECO_NAME~variable, function(x) length(unique(x)))

# by hand -
# make new variable of ECO_NAME*variable combinations:
dfm$variable2 <- as.factor(paste(dfm$variable, dfm$ECO_NAME, sep = " - "))

# make vector to collect results for all unique ECO_NAME*variable combinations:
Ns <- data.frame(count = rep(NA, length(unique(dfm$variable2))),
                row.names = unique(dfm$variable2))

# loop through all unique ECO_NAME*variable combinations:
for (i in levels(dfm$variable2)){
                                subset = dfm$value[dfm$variable2 == i]
				 Ns[i, "count"] <- length(unique(subset))
				}

# put counts in matrix/table, Ns$count is in order of taxonomy
# levels (= "variable"), so I have to fill by cols (byrow = F),
# as the matrix/table colums are chosen to be the taxonomy levels:
result <- matrix(Ns$count,
                nrow = length(levels(dfm$ECO_NAME)),
                ncol = length(levels(dfm$variable)),
                dimnames = list(levels(dfm$ECO_NAME), levels(dfm$variable)),
                byrow = F)

print(results)
###


Zitat von Chris Mcowen <chrismcowen at gmail.com>: