[R] zeros keep dropping
Peter Dalgaard
p.dalgaard at biostat.ku.dk
Mon Oct 26 19:25:44 CET 2009
Lanna Jin wrote:
> Hello All!
>
> I am trying to plot the frequency of species coocurrance.
> If given a data set similar like this...(V1="species A", V2="species B",
> V3="frequency of cooccurance")
>
>> data
> V1 V2 V3
> 1 A B 0
> 2 A C 2
> 3 A D 5
> 4 B C 0
> 5 B D 1
> 6 C D 0
>> data1<-as.data.frame(lapply(data,function(x)(rep(x,data$V3))))
>> as.data.frame(data1[-1])
>> fdata<-ftable(as.data.frame(data1[-3]))
>> fdata
> V2 B C D
> V1
> A 0 2 5
> B 0 0 1
> C 0 0 0
>
> Question 1: How can I create a matrix or (contingency) table that would
> include "A, B, C and D" in columns and "A, B, C, and D" in the rows (through
> R, not manually adjusting the original data text file).
> - When I "lapply(data,function(x)(rep(x,data$V3)))", the rows with V3=0 are
> dropped.
>
> Question 2: How would I graph the frequency of the counts (V3) (would look
> most likely follow the poisson distribution).
> I've used plot(table(data$V3)) to get the correct graph, but is there a way
> to graph the frequency from a contingency table (question 1) to elicit the
> same graph?
>
> Thanks in advance for your help...
I think you need an intervening
all.levels <- c("A", "B", "C", "D") # or LETTERS[1:4]
data$V1 <- factor(data$V1, levels=all.levels)
data$V2 <- factor(data$V2, levels=all.levels)
and it is probably better to use something like
data1 <- data[rep(1:6, data$V3),]
That is, if you insist on the strategy of replicating rows.
You may prefer xtabs(V3~V1+V2, data=data).
(In all cases "or something like that". I haven't actually tested.)
--
O__ ---- Peter Dalgaard Øster Farimagsgade 5, Entr.B
c/ /'_ --- Dept. of Biostatistics PO Box 2099, 1014 Cph. K
(*) \(*) -- University of Copenhagen Denmark Ph: (+45) 35327918
~~~~~~~~~~ - (p.dalgaard at biostat.ku.dk) FAX: (+45) 35327907
More information about the R-help
mailing list