Reputation: 559
Alright so I have a dataframe of some number of rows and 44 columns.
I am trying, in order to make a heatmap, to create a matrix that is 5 x 44.
x = seq(1,5,1)
y = seq(1,44,1)
f = function(x, y) { r = mean(r01[r01$weekday == x,y]); r}
z = outer(x, y, f)
Is how I've been trying to do it, but I get a couple of errors:
Error in dim(robj) <- c(dX, dY) :
dims [product 220] do not match the length of object [1]
In addition: Warning messages:
1: In r01$weekday == p :
longer object length is not a multiple of shorter object length
2: In mean.default(r01[r01$weekday == p, q]) :
argument is not numeric or logical: returning NA
So in my attempts to remove these errors, I have tried the following:
x = seq(1,5,1)
y = seq(1,44,1)
f = function(x,y){ r = x+y; r+1 }
z = outer(x, y, f)
Which works. Now this, which doubles as my reproducible example:
hugs<-data.frame(replicate(44,sample(1:5,1000,rep=TRUE)))
x = seq(1,5,1)
y = seq(1,44,1)
f = function(x, y) { r = mean(hugs[hugs$x1 == x,y]); r}
z = outer(x, y, f)
Fails, with largely the same errors:
Error in dim(robj) <- c(dX, dY) :
dims [product 220] do not match the length of object [1]
In addition: Warning message:
In mean.default(hugs[hugs$X1 == x, y]) :
argument is not numeric or logical: returning NA
And this leaves me largely confused, because I assumed this would work if the one above it did. So I have no clue how to reconcile what works from the one that works with the ones that don't.
The error that's missing from the "hugs" version I reproduced by modifying hugs with the following before running the rest:
hugs$X1[hugs$X1 == 1,] <- 3
But I don't actually know how to fix that error, since my original dataframe has values from 1:5 in weekday, and 44 columns just like my example, so I must just be misunderstanding its source. It's not that the means don't exist:
for (i in x){
for (j in y){
print(mean(r01[r01$weekday == i,j]))}}
Returns values for every single one. I guess I could reconstruct my upper logic to just use these two loops, but I want to use outer since it's more efficient(?).
Upvotes: 1
Views: 43
Reputation: 559
Thanks to the comments, I decided not to use outer and instead made use of this equally fast and simple one liner:
aggregate(r01[1:44], r01["weekday"], mean)
Upvotes: 1