Quantcast
Channel: Active questions tagged r - Stack Overflow
Viewing all articles
Browse latest Browse all 206342

Matrix Multiplier Error - Switching Max to Sum

$
0
0

I have two dataframes one with information on animal found in one location (df1) and another with information on the characteristic of the animal (df2)

I am trying to use the matrix multiplier (%*%) and apply functions such as 'sum' and 'max' and obtain the information in each location.

For Eg:

df1:
 Location No. Dog Cat Cow Sheep
    1            0   2   2   1
    2            0   1   0   1
    3            0   0   0   1
    4            0   0   2   1

df2:
Name of Animal  BodySize   FavoriteScore
Dog             40         10
Cat             20         08
Cow             100        05
Sheep           60         07

My objective is to obtain information such as 1. How many types of animals are there per location? 2. What is the largest animal found in each location? (where in it compares across all the animals and picks the largest bodysize and delivers it) 3. Which animal has the highest FavoriteScore in each location? (where in it compares across all the animals and picks the largest favoritescore and delivers it)

To obtain this I am using the following code:

Typemaker <- function (n) {
  o<-sum(n>0)
  return(o)
} 

apply(df1[,1:4] ,1, Typemaker)
df1$sumtype <- apply(df1[,2:5] ,1, Typemaker)

and 
Favoritemaker <- function (n) {
  o<- max(n %*% df2$FavoriteScore)
  return(o)
} 
apply(df1[,1:4] ,1, Favoritemaker)
df1$Favorite <-  apply(df1[,2:5] ,1, Favoritemaker)
or 
Bodysizemaker <- function (n) {
  o<- max(n %*% df2$BodySize)
  return(o)
} 
apply(df1[,1:4] ,1, Bodysizemaker).
df1$Bodysize <- apply(df1[,2:5] ,1, Bodysizemaker).

So that my answer would appear as:

 Location No. Dog Cat Cow Sheep Type Favorite Bodysize
    1            0   2   2   1     3    08       100
    2            0   1   0   1     2    08       20
    3            0   0   0   1     1    07       60
    4            0   0   2   1     2    07       100

However, the bodysize and favoritescores are being provided as a total of the row rather the largest value.

I am not sure of what exactly is the issue.

I would be grateful for any help.


Viewing all articles
Browse latest Browse all 206342

Trending Articles