Skip to content

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:
#
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
____________________________________________________________
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
____________________________________________________________
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
--
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.