Sudatta
Sudatta

Reputation: 73

Accessing values from prop.table() in R

I have a probability table as follows:

prop.table(table(h[[10]]))

         1         12         15         16          3          9 
0.20000000 0.30000000 0.10000000 0.13333333 0.20000000 0.06666667 

I then converted it to matrix using:

probData <- data.matrix(prop.table(table(h[[10]])))

Now I need to run the condition such as:

if (probData[row,column] > 0.2){

  print(print name of that column eg 1,12,15,16)  

}

probData[row,column] gives me the probability.The problem is I am not able to access the column names (1,12,15,16) from their respective probabilities. Any help is appreciated.

Upvotes: 1

Views: 2540

Answers (1)

Rich Scriven
Rich Scriven

Reputation: 99331

You could call the names directly from the prop.table with a vectorized condition.

Here's an example with the mtcars data

(p <- prop.table(table(mtcars[1:10,3])))
#
#  108 140.8 146.7   160 167.6   225   258   360 
#  0.1   0.1   0.1   0.2   0.1   0.1   0.1   0.2 
# 
names(p[p > 0.1])
# [1] "160" "360"

Or in one line, you could do

names((p <- prop.table(table(mtcars[1:10,3])))[p > 0.1])
# [1] "160" "360"

Upvotes: 2

Related Questions