Pergunta

Here is my dataframe df:

x y

1 1 1
2 1 2
3 2 3

And I get the result:

2
3

with the code:

tapply(df$y, df$x, sum)

But I expect to get

3
3

Is there something wrong with my code?

Foi útil?

Solução

You're getting the correct answer, just reading it wrong.

look at the whole output:

> tapply(df$y, df$x, sum)
1 2 
3 3 

Your groups are 1 and 2 and the values are 3 and 3. Is horizontal. You can see this if you try tapply in the other direction

> tapply(df$x, df$y, sum)
1 2 3 
1 1 2
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top