R replace the value with frequency

I am trying to replace the values ​​in a data frame with frequency.

Here are my details:

blah<-list(c(1,1,2,2,3,1,3,2,2,5,5), c(7,8,7,8,9,9,7,8,9,7,7)) blah<-as.data.frame(blah) colnames(blah)<-c("col1","col2") 

I created a table with two columns.

Next, I use the β€œtable” to generate the frequency for both columns:

 col1Freq<-table(blah[,1])/dim(blah)[1] col2Freq<-table(blah[,2])/dim(blah)[1] 

My goal is to replace all blah values ​​with frequencies. So, the final table should be the same size as blah, but I want frequencies instead of integers.

Sorry, I do not have photos to show .... Thanks for your help !!!!

+4
source share
2 answers

If I understand your question correctly, the basic function R ave() (ignore its misleading name) will do what you are looking for.

 blah2 <- transform(blah, col1Freq = ave(col1, col1, FUN=function(X) length(X)/nrow(blah)), col2Freq = ave(col2, col2, FUN=function(X) length(X)/nrow(blah))) blah2[3:4] # col1Freq col2Freq # 1 0.2727273 0.4545455 # 2 0.2727273 0.2727273 # 3 0.3636364 0.4545455 # 4 0.3636364 0.2727273 # 5 0.1818182 0.2727273 # 6 0.2727273 0.2727273 # 7 0.1818182 0.4545455 # 8 0.3636364 0.2727273 # 9 0.3636364 0.2727273 # 10 0.1818182 0.4545455 # 11 0.1818182 0.4545455 
+4
source

I ran into the same problem. In my case, I need such a transformation for the subsequent calculation of the product of frequencies for each column, which should lead to the frequency (probability) of multidimensional (multidimensional) data.

My solution works for any number of columns:

 apply(blah,2,function(x){ t = as.data.frame(table(x)) t$Freq[match(x,t[,1])]/length(x) }) 
+1
source

All Articles