Functions R - Strange Character

3

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

Here is the function:

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

When used, xx appears above the returned table.

Can you explain why?

    
asked by anonymous 19.12.2017 / 17:28

1 answer

4

You are using the table() function that results in a table object in the R that contains 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 name of the object, in case "m":

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

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

Removing this character will depend on the format you want in the function return. For example, if you want to continue with the format table you can use the dnn = '' argument (which also allows you to rename 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
    
19.12.2017 / 18:02