[R] summary tables with tapply
Gabor Grothendieck
ggrothendieck at gmail.com
Sat Mar 11 15:33:16 CET 2006
I am not sure I understand the question but is the situation that
you have two vectors: x and y such that for each level of y
x is constant so that for each level of y you want to find that value
of x? In that case:
x <- c("A", "A", "A", "B", "B")
y <- c(1,1,2,3,3)
unique(data.frame(x,y))
or
tapply(x, y, head, 1)
or
tapply(x, y, "[", 1)
or if x is a factor
xf <- factor(x)
tapply(as.character(xf), y, head, 1)
Aside. I suggest you use dput to display your data in your post to
make it easier for
others to read it back into R, e.g.
> dput(x)
c("A", "A", "A", "B", "B")
and editing that allows one to post this:
x <- c("A", "A", "A", "B", "B")
On 3/11/06, Stefanie von Felten, IPW&IfU <sfelten at uwinst.unizh.ch> wrote:
> Hello everyone,
>
> #I have a long table with a factor Plotfac:
> > Plotfac[1:20]
> [1] 1 1 1 1 2 2 2 2 3 3 3 3 4 4 4 4 5
> 5 6 6
>
> #there is the species diversity of each plot (Plotfac):
> DataSort$div[1:20]
> [1] 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 2 2 2 2
>
> #and the plant community growing in each plot:
> > DataSort$community[1:20]
> [1] DLRT_1 DLRT_1 DLRT_1 DLRT_1 DLRT_1 DLRT_1 DLRT_1 DLRT_1
> DLRT_1 DLRT_1 DLRT_1 DLRT_1 DLRT_1 DLRT_1 DLRT_1 DLRT_1 ARR HOL
> [18] ARR HOL ARR HOL ARR HOL
>
> #Now I want to have a summary table with data on the plotlevel
> #I define the new plot variable:
> Newplot<-levels(Plotfac)#a vector with all plotnumbers 1-n)
> >Newplot[1:10]
> [1] "1" "2" "3" "4" "5" "6" "7" "8" "15" "16"
>
> #the new div variable (div is numeric, so mean is no problem)
> PlotDiv<-tapply(DataSort$div,Plotfac,mean)
> > PlotDiv[1:10]
> 1 2 3 4 5 6 7 8 15 16
> 4 4 4 4 2 2 2 2 2 2
>
> #BUT HOW CAN I DO THIS WITH THE CHARACTER COMMUNITY?
> #A rather unsatisfactory solution:
> community<-tapply(DataSort$community,Plotfac,as.character)
> results in:
> community[1:5]
> $"1"
> [1] "DLRT_1" "DLRT_1" "DLRT_1" "DLRT_1"
>
> $"2"
> [1] "DLRT_1" "DLRT_1" "DLRT_1" "DLRT_1"
>
> $"3"
> [1] "DLRT_1" "DLRT_1" "DLRT_1" "DLRT_1"
>
> $"4"
> [1] "DLRT_1" "DLRT_1" "DLRT_1" "DLRT_1"
>
> $"5"
> [1] "ARR HOL" "ARR HOL"
> #how can I make community look like
> "DLRT_1", DLRT_1","DLRT_1","DLRT_1","ARR HOL"...
> #without repeating the community of each observation within a plot? Is
> it possible to extract a single "value" out of these repeated structures?
>
> Thanks for any suggestions!
>
> Steffi
>
>
> --
> ---------------------------------
> Stefanie von Felten
> Doktorandin
>
> ETH Zürich
> Institut für Pflanzenwissenschaften
> ETH Zentrum, LFW A 2
>
> Telefon: 044 632 85 97
> Telefax: 044 632 11 53
> e-mail: stefanie.vonfelten at ipw.agrl.ethz.ch
> http://www.ipw.agrl.ethz.ch/~svfelten/
>
> und:
>
> Universität Zürich
> Institut für Umweltwissenschaften
> Winterthurerstrasse 190
> 8057 Zürich
>
> Telefon: 044 635 61 23
> Telefax: 044 635 57 11
> e-mail: sfelten at uwinst.unizh.ch
> http://www.unizh.ch/uwinst/homepages/steffi.html
>
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
>
More information about the R-help
mailing list