cplus
cplus

Reputation: 1115

prop.table error: x is not an array

I have the following data frame:

Categories  Var1    Var2    Var3
Conan   7,979510425 11,95024652 3,06642437
Conan   7,819749969 12,34289817 3,5677732
Conan   8,41786203  12,35299044 3,142838395
Kylee   8,149103463 12,15152268 2,919922332
Kylee   7,960192219 11,84316143 2,790122653
Kylee   8,145724491 11,95609623 2,894319091
Breanna 7,705270648 12,12028502 3,040245578
Breanna 8,033576739 12,048678   3,060184406
Breanna 8,049027753 12,04192098 3,492948654
Pandora 8,071376899 12,30834624 3,058397935
Pandora 8,246880683 12,65372891 2,761259858
Pandora 7,8022713   11,8658064  2,606787691

I am trying to get the prop.table for it:

dataprop <- prop.table(rowsum(data[,2:4],data[,1]),1)

Error in margin.table(x, margin) : 'x' is not an array

could you please let me know what I am missing here? thanks

Upvotes: 6

Views: 9332

Answers (1)

Zheyuan Li
Zheyuan Li

Reputation: 73285

Because rowsum is a generic function, which has default method for matrix, as well as method for data frame. You gave it a data frame, so it returns a data frame, but prop.table can not take a data frame. The fix is

prop.table(data.matrix(rowsum(...)), 1)

Upvotes: 8

Related Questions