An embedded and charset-unspecified text was scrubbed... Name: not available URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20120510/c7595534/attachment.pl>
averaging two tables (rows with columns)
6 messages · Sarah Goslee, Kristi Glover, John Kane +1 more
Kristi, Several people have already suggested you use dput() to provide your data. In R, dput(table1) dput(table2) Then copy and paste the output of those commands into your email. There's no way to reliably copy and paste your raw data: using dput() is the best way to provide it. Sarah On Thu, May 10, 2012 at 1:18 PM, Kristi Glover
<kristi.glover at hotmail.com> wrote:
Hi R-usuar, I still find the table is not in readable formate. I now forwarded the email.hope it works. I sent it to my account . It was perfect- but when I sent it to R help- then it comes with unreadable formate. This is my last try. If not, then I will try to do in Excel manually. cheers, From: kristi.glover at hotmail.com To: kristi.glover at hotmail.com Subject: RE: [R] averaging two tables (rows with columns) Date: Thu, 10 May 2012 14:06:41 -0300 Hi John , Petr and R userI am sorry that my data was not readable formate in the last email. Agin I am trying to send it. hope this time, that ?table can be readable. As I mentioned earlier that I was struggling to figure out on how I can calculate the average from the two tables in R. Any one can help me? really your helpwould be grateful- I am spending so much time to figure it out. It should not be so hard, I think.I have very big data but I have created a hypothetical data for simplification.for example I have : table 1 Table 1: species occurrence data> table1 ? ? ?X speciesX speciesY speciesZ speciesXX1 Plot1 ? ? ? ?1 ? ? ? ?0 ? ? ? ?1 ? ? ? ? 02 Plot2 ? ? ? ?0 ? ? ? ?1 ? ? ? ?1 ? ? ? ? 03 plot3 ? ? ? ?1 ? ? ? ?0 ? ? ? ?0 ? ? ? ? 14 plot4 ? ? ? ?0 ? ? ? ?0 ? ? ? ?1 ? ? ? ? 0 Table 2: table 2. species tolerance data
table2 ? ? ? ? ? ? ? ? ?X EnviA EnviB EnviC1 ?SpeciesX ?0.21 ?0.40 ?0.172 ?SpeciesY ?0.10 ?0.15 ?0.183 SpeciesXY ?0.14 ?0.16 ?0.19
You may noticed that table 2 does not have species Z which was in table Table 3: Now I want to get the average value of species tolerance in each plot based on each environmental value (EnviA or EnviB etc).The example of the out come (final table I was looking for it). Results table 1a: averages species tolerance in each plot based on EnviA such as:
table3.a ? ? ?X speciesX speciesY speciesZ speciesXX average1 plot1 ? ? 0.21 ? ? ? NA ? Nodata ? ? ?0.14 ? 0.1752 plot2 ? ? ? NA ? ? ?0.1 ? Nodata ? ? ? ?NA ? 0.1003 plot3 ? ? ? NA ? ? ? NA ? Nodata ? ? ?0.14 ? 0.1404 plot4 ? ? 0.21 ? ? ? NA ? Nodata ? ? ? ?NA ? 0.210Table 4
table3.b: Result table 1b: average species tolerance in plot based on EnviB ? ? ?X speciesX speciesY speciesZ speciesXX average1 plot1 ? ? ?0.4 ? ? ? NA ? Nodata ? ? ?0.16 ? ?0.282 plot2 ? ? ? NA ? ? 0.15 ? Nodata ? ? ? ?NA ? ?0.153 plot3 ? ? ? NA ? ? ? NA ? Nodata ? ? ?0.16 ? ?0.164 plot4 ? ? ?0.4 ? ? ? NA ? Nodata ? ? ? ?NA ? ?0.40
I hope this time the data would be readable formate. Would any one help me how I can calculate these?Thanks Kristi Golver==
Date: Thu, 10 May 2012 08:01:11 -0800 From: jrkrideau at inbox.com Subject: RE: [R] averaging two tables (rows with columns) To: kristi.glover at hotmail.com; r-help at r-project.org Kristi, Your data has come through in a totally unreadable layout. Have a look at ?dput as a handy way to present sample data. ?Just run dput(mydata), copy the output and paste into the email. John Kane Kingston ON Canada
An embedded and charset-unspecified text was scrubbed... Name: not available URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20120510/c0778ada/attachment.pl>
Kristi
The tables , if read in edit mode are vagely readable but please just do as Petr and I suggest.
Use the dput command.
If you have the table in an R data.frame all you need to do is use the command dput(mytable)
I read your two tables into R and did that . Note I called them dat1 & dat2. table is a command in R and it is best not to use it as a variable name.
Results
1> dput(dat1)
structure(list(X = c("Plot1", "Plot2", "plot3", "plot4"), 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))
1> dput(dat2)
structure(list(X = c("SpeciesX", "SpeciesY", "SpeciesXY"), 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("1",
"2", "3"))
1>
Now you just need to stick variable name and an <- in front of the dput material and paste it into R
Like THIS
mytable1 <- structure(list(X = c("Plot1", "Plot2", "plot3", "plot4"), 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))
and paste it into R.
Now we probably can get somewhere.
John Kane
Kingston ON Canada
-----Original Message----- From: kristi.glover at hotmail.com Sent: Thu, 10 May 2012 14:18:50 -0300 To: r-help at r-project.org Subject: [R] FW: averaging two tables (rows with columns) Hi R-usuar, I still find the table is not in readable formate. I now forwarded the email.hope it works. I sent it to my account . It was perfect- but when I sent it to R help- then it comes with unreadable formate. This is my last try. If not, then I will try to do in Excel manually. cheers, From: kristi.glover at hotmail.com To: kristi.glover at hotmail.com Subject: RE: [R] averaging two tables (rows with columns) Date: Thu, 10 May 2012 14:06:41 -0300 Hi John , Petr and R userI am sorry that my data was not readable formate in the last email. Agin I am trying to send it. hope this time, that table can be readable. As I mentioned earlier that I was struggling to figure out on how I can calculate the average from the two tables in R. Any one can help me? really your helpwould be grateful- I am spending so much time to figure it out. It should not be so hard, I think.I have very big data but I have created a hypothetical data for simplification.for example I have : table 1 Table 1: species occurrence data> table1 X speciesX speciesY speciesZ speciesXX1 Plot1 1 0 1 02 Plot2 0 1 1 03 plot3 1 0 0 14 plot4 0 0 1 0 Table 2: table 2. species tolerance data
table2 X EnviA EnviB EnviC1 SpeciesX 0.21 0.40
0.172 SpeciesY 0.10 0.15 0.183 SpeciesXY 0.14 0.16 0.19 You may noticed that table 2 does not have species Z which was in table Table 3: Now I want to get the average value of species tolerance in each plot based on each environmental value (EnviA or EnviB etc).The example of the out come (final table I was looking for it). Results table 1a: averages species tolerance in each plot based on EnviA such as:
table3.a X speciesX speciesY speciesZ speciesXX average1 plot1 0.21 NA Nodata 0.14 0.1752 plot2 NA 0.1 Nodata NA 0.1003 plot3 NA NA Nodata 0.14 0.1404 plot4 0.21 NA Nodata NA 0.210Table 4
table3.b: Result table 1b: average species tolerance in plot based on EnviB X speciesX speciesY speciesZ speciesXX average1 plot1 0.4 NA Nodata 0.16 0.282 plot2 NA 0.15 Nodata NA 0.153 plot3 NA NA Nodata 0.16 0.164 plot4 0.4 NA Nodata NA 0.40
I hope this time the data would be readable formate. Would any one help me how I can calculate these?Thanks Kristi Golver==
Date: Thu, 10 May 2012 08:01:11 -0800 From: jrkrideau at inbox.com Subject: RE: [R] averaging two tables (rows with columns) To: kristi.glover at hotmail.com; r-help at r-project.org Kristi, Your data has come through in a totally unreadable layout. Have a look at ?dput as a handy way to present sample data. Just run dput(mydata), copy the output and paste into the email. John Kane Kingston ON Canada
[[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.
____________________________________________________________ GET FREE SMILEYS FOR YOUR IM & EMAIL - Learn more at http://www.inbox.com/smileys Works with AIM?, MSN? Messenger, Yahoo!? Messenger, ICQ?, Google Talk? and most webmails
Well I'd suggest learning to use the return key. It really helps improve the readability of a posting. Three of the for files came through okay but table3a has a problem. It looks like some kind of extraneous charater(s) got into it but a second or so of editing makes it work so we have all 4 tables now. John Kane Kingston ON Canada
-----Original Message-----
From: kristi.glover at hotmail.com
Sent: Thu, 10 May 2012 14:36:51 -0300
To: r-help at r-project.org
Subject: Re: [R] FW: averaging two tables (rows with columns)
oppps, Now I used 'dput' function. Again I am sending. I am so sorry for
inconvenience.
HI R userI am sorry that my data was not readable formate in the last
email. Agin I am trying to send it. hope this time, that table can be
readable.As I mentioned earlier that I was struggling to figure out on
how I can calculate the average from the two tables in R. Any one can
help me? really your helpwould be grateful- I am spending so much time to
figure it out. It should not be so hard, I think.I have very big data but
I have created a hypothetical data for simplification.for exampleI have :
table 1Table 1: species occurrence data> dput(table1)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))
Table 2: table 2. species tolerance data> dput(table2)structure(list(X =
structure(c(1L, 3L, 2L), .Label = c("SpeciesX", "SpeciesXY", "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))> You may noticed
that table 2 does not have species Z which was in tableTable 3: Now I
want to get the average value of species tolerance in each plot based on
each environmental value (EnviA or EnviB etc).The example of the out come
(final table I was looking for it).Results table 3a: averages species
tolerance in each plot based on EnviAsuch as: >
dput(table3a)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), av!
erage = 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))
Table 3b
Result table 3b: average species tolerance in plot based on EnviB>
dput(table3b) 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 this time the data would be readable
formate. Would any one help me how I can calculate these?ThanksKristi
Golver==
again
[[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.
____________________________________________________________ Send your photos by email in seconds... TRY FREE IM TOOLPACK at http://www.imtoolpack.com/default.aspx?rc=if3 Works in all emails, instant messengers, blogs, forums and social networks.
Hello,
After all the trouble, workable datasets.
I have a doubt on what you want.
By looking at the two results data.frames, I don't believe they match the
problem description.
The average columns are wrong. Look at line 1 in table3a. I has speciesXX
with a value of 0.14
but speciesXX does NOT occur in table2.
First Error ---> You are using the value of SpeciesXY in table2.
And even if speciesXX and SpeciesXY were the same, the value in table1,
Plot1 is zero.
Second Error ---> You are counting values that are not in the plots.
If I'm right, and only in that case, try the following
fun <- function(df1, df2){
y <- df2[, -1]
y <- t(y)
colnames(y) <- tolower(df2[, 1])
names(df1) <- tolower(names(df1))
cnames <- intersect(names(df1), colnames(y))
x <- as.matrix(df1[, cnames])
y <- as.matrix(y[, cnames])
count <- apply(x, 1, sum, na.rm=TRUE)
res <- (x %*% t(y))/count
rownames(res) <- df1[, 1]
res
}
fun(table1, table2)
Now, correct the first error, assuming it's the species name in table1
that's wrong.
tb1 <- table1
colnames(tb1)[5] <- "speciesXY"
tb1
fun(tb1, table2)
And correct the second, recording speciesXY (or XX) as 1 in Plot1.
tb1$speciesXY[1] <- 1
fun(tb1, table2)
# output
EnviA EnviB EnviC
Plot1 0.175 0.28 0.18
Plot2 0.100 0.15 0.18
plot3 0.175 0.28 0.18
plot4 NaN NaN NaN
Finally, as you can see, the output of fun() is in a different format.
It is possible to change that (obvious) but not worth the trouble if I'm
wrong.
Give some feedback.
Hope this helps,
Rui Barradas
Kristi Glover wrote
oppps, Now I used 'dput' function. Again I am sending. I am so sorry for
inconvenience.
HI R userI am sorry that my data was not readable formate in the last
email. Agin I am trying to send it. hope this time, that table can be
readable.As I mentioned earlier that I was struggling to figure out on how
I can calculate the average from the two tables in R. Any one can help me?
really your helpwould be grateful- I am spending so much time to figure it
out. It should not be so hard, I think.I have very big data but I have
created a hypothetical data for simplification.for exampleI have : table
1Table 1: species occurrence data> dput(table1)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))
Table 2: table 2. species tolerance data> dput(table2)structure(list(X =
structure(c(1L, 3L, 2L), .Label = c("SpeciesX", "SpeciesXY", "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))> You may noticed that table
2 does not have species Z which was in tableTable 3: Now I want to get the
average value of species tolerance in each plot based on each
environmental value (EnviA or EnviB etc).The example of the out come
(final table I was looking for it).Results table 3a: averages species
tolerance in each plot based on EnviAsuch as: >
dput(table3a)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), av!
erage = 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))
Table 3b
Result table 3b: average species tolerance in plot based on EnviB>
dput(table3b) 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 this time the data would be readable
formate. Would any one help me how I can calculate these?ThanksKristi
Golver==
again
[[alternative HTML version deleted]]
______________________________________________ R-help@ 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.
-- View this message in context: http://r.789695.n4.nabble.com/averaging-two-tables-rows-with-columns-tp4623845p4624300.html Sent from the R help mailing list archive at Nabble.com.