R compute percentage values in data frame

后端 未结 1 909
轮回少年
轮回少年 2020-12-20 00:46

My question today refers to a data frame I\'m working on in R. The header of the data frame looks like the following: String(unique), Integer N[0-23]

Those 24 Integ

相关标签:
1条回答
  • 2020-12-20 01:14

    Use prop.table on a matrix containing the values:

    x <- data.frame(id=letters[1:3],val0=1:3,val1=4:6,val2=7:9)
    prop.table(as.matrix(x[-1]),margin=1)
               val0      val1      val2
    [1,] 0.08333333 0.3333333 0.5833333
    [2,] 0.13333333 0.3333333 0.5333333
    [3,] 0.16666667 0.3333333 0.5000000
    

    Edit: A fully working example:

    tt=read.table("topichitsperhod.csv",sep=",",header=TRUE)  
    tt=na.omit(tt[-1])
    pt=prop.table(tt[-1],margin=NULL)
    

    First column is being left out because it held the topic strings.

    0 讨论(0)
提交回复
热议问题