Functions R - Strange Character

Asked

Viewed 92 times

4

I created a function in the R and detected a strange character appearing on the return of the function. Can you explain to me?

Here is the function:

funcao_categorica <- function(xx){
  rr <- prop.table(table(xx))*100
  return(rr)
}

When used, appears the xx above the returned table.

Can you tell me why?

1 answer

5


You are using the function table() which results in an object table R containing a name for each dimension.

For example, with the following Matrix m:

m <- matrix(1:8, 4)
> matrix(1:8, 4)
       [,1] [,2]
 [1,]    1    5
 [2,]    2    6
 [3,]    3    7
 [4,]    4    8

The function table(m) will have the object name, in the case "m":

> table(m)
m
1 2 3 4 5 6 7 8
1 1 1 1 1 1 1 1

So the strange character is simply the name of the object used.

Taking this character will depend on the format you want when returning the function. For example, if you want to continue with the format table you can use the argument dnn = '' (which also allows you to rename the table:

> prop.table(table(m, dnn = ''))

    1     2     3     4     5     6     7     8
0.125 0.125 0.125 0.125 0.125 0.125 0.125 0.125

Or you can simply convert the object to a data frame:

> data.frame(prop.table(table(m)))
  m  Freq
1 1 0.125
2 2 0.125
3 3 0.125
4 4 0.125
5 5 0.125
6 6 0.125
7 7 0.125
8 8 0.125
  • Perfect. The function parameter ends up being the name of the dimension used. I understood. Are these specifications listed in the documentation? Where did you locate them?

  • Specifications are in the documentation: ?table

Browser other questions tagged

You are not signed in. Login or sign up in order to post.