how to do averaging of two tables (rows with columns)
Based upon my understanding of your problem, this should do the job:
d3 <- dat1 d3[2] <- ifelse( d3[2] == 1, dat2[1,2], NA ) d3[3] <- ifelse( d3[3] == 1, dat2[2,2], NA ) d3[4] <- "Nodata" d3[5] <- ifelse( d3[5] == 1, dat2[3,2], NA ) d3$average <- rowMeans( d3[c(2,3,5)], na.rm = TRUE )
d3 is now the same as your dat3. This is cumbersome, but maybe a starting point. Rgds, Rainer
On Friday 11 May 2012 01:35:18 Kristi Glover wrote:
Hi R user,
I saw some errors in the dat1.
The correct dat1 is
dat1 <- structure(list(X = structure(1:4, .Label = c("Plot1",
"Plot2", "plot3", "plot4"), class = "factor"), speciesX = c(1L, 0L, 0L, 1L), speciesY =
c(0L, 1L, 0L, 0L), speciesZ = c(1L, 1L, 0L, 1L), speciesXX = c(1L, 0L, 1L, 0L)), .Names =
c("X", "speciesX", "speciesY", "speciesZ", "speciesXX"), class = "data.frame", row.names =
c(NA, -4L))
cheers,
K
From: kristi.glover at hotmail.com
To: r-help at r-project.org
Date: Thu, 10 May 2012 19:03:38 -0300
Subject: [R] how to do averaging of two tables (rows with columns)
Hi R user,
I finally able to send you the table in
readable format. I have seen that some of you do send tables in email
when asking questions, but why i could not send. Any way
some of you helped me to send you the example table in a readable format.
now,
I want to concentrate on my problem. I am trying to get the information
(dat 3) from dat 1 and 2 in R. I have very big data but these data are
just hypothetical data. my data structures are exactly same as dat 1
and dat 2. I created dat 3 and dat4 manually to show what information I
wanted to have.
I am struggling to figure it out how I can do in R. I think it is not difficult. I hope any one can help me.
dat1 is the table of species occurrence (o means species absence, 1 means species presence).
dat1 <- structure(list(X = structure(1:4, .Label = c("Plot1",
"Plot2", "plot3", "plot4"), class = "factor"), speciesX = c(1L, 0L, 1L, 0L), speciesY =
c(0L, 1L, 0L, 0L), speciesZ = c(1L, 1L, 0L, 1L), speciesXX = c(0L, 0L, 1L, 0L)), .Names =
c("X", "speciesX", "speciesY", "speciesZ", "speciesXX"), class = "data.frame", row.names =
c(NA, -4L))
dat2 is the species tolerances value in each environmental variable
dat2 <- structure(list(X = structure(c(1L,
3L, 2L), .Label = c("SpeciesX", "SpeciesXX", "SpeciesY"), class = "factor"), EnviA =
c(0.21, 0.1, 0.14), EnviB = c(0.4, 0.15, 0.16), EnviC = c(0.17, 0.18, 0.19)), .Names =
c("X", "EnviA", "EnviB", "EnviC"), class = "data.frame", row.names = c(NA, -3L))
## note (here in dat 2 there is no "species Z" you can see that )
Now, I want to get the average value of tolerances in each grid. like dat 3
the dat3 is based on the column EnviA.
dat3 <-structure(list(X = structure(1:4, .Label = c("plot1",
"plot2", "plot3", "plot4"), class = "factor"), speciesX = c(0.21, NA, NA,
0.21), speciesY = c(NA, 0.1, NA, NA), speciesZ = structure(c(1L, 1L, 1L,
1L), .Label = "Nodata", class = "factor"), speciesXX = c(0.14, NA, 0.14,
NA), average = c(0.175, 0.1, 0.14, 0.21)), .Names = c("X", "speciesX",
"speciesY", "speciesZ", "speciesXX", "average"), class = "data.frame",
row.names = c(NA, -4L))
dat4 is same thing as dat3 but here i used EnviB instead of EnviA.
dat4 <- structure(list(X = structure(1:4, .Label = c("plot1", "plot2", "plot3", "plot4"), class =
"factor"), speciesX = c(0.4, NA, NA, 0.4), speciesY = c(NA, 0.15, NA, NA), speciesZ =
structure(c(1L, 1L, 1L, 1L), .Label = "Nodata", class = "factor"), speciesXX = c(0.16, NA,
0.16, NA), average = c(0.28, 0.15, 0.16, 0.4)), .Names = c("X", "speciesX", "speciesY",
"speciesZ", "speciesXX", "average"), class = "data.frame", row.names = c(NA, -4L))
I hope you understand my problem and you can help me.
Thanks
Kristi
[[alternative HTML version deleted]]
______________________________________________ R-help at r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
[[alternative HTML version deleted]]
______________________________________________ R-help at r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.