R语言排序与统计频数

R 语言排序

数据示意

FL <- c(1,5,4,4)
FF <- c(3,7,4,2)
FA <- c("A","C","D","B")
ora  <- data.frame(FL,FF,FA)

排序

##只排一行
sort(ora$FL)
sort(ora$FL,decreasing=TRUE)

获得排序的位置,多列排序,升序以及降序

 order(ora$FL)
 ora[order(ora$FL),]
 ora[order(-ora$FL),]
 ora[order(ora$FL,ora$FA),]
 ora[order(ora$FL,ora$FA,decreasing=T),]
 ora[order(-ora$FL,ora$FA),]

还原排序

ora2 <-ora[order(ora$FL),]  
ora3 <- ora2[order(order(ora$FL)),]

利用行名还原排序

ora2 <-ora[order(ora$FL),]  
N <- nrow(ora)
DealData <- data.frame(FL=numeric(N),FF=numeric(N),FA=numeric(N))       
DealData[as.integer(row.names(ora2)),] <- ora2

按照自定义列排序

ora
ora4 <- cbind(ora2,ora2$FA)
rownames(ora4) <- NULL
merge(ora,ora4,all=TRUE,sort=FALSE)
merge(ora4,ora,all=TRUE,sort=FALSE)

R语言统计频率

数据

score    <- c(1,2,1,3,3,4)
grade <- c("A","A","A","B","B","C")
LA <- data.frame(score ,grade ,stringsAsFactors=F)

统计一列的数据

sp <- table(LA$score)
sp <- as.data.frame(sp,stringsAsFactors=F)
str(sp)

统计两列的或者多列的组合

  • 方法一
    install.packages("plyr"),没有这个包的话要下载
    library(plyr)
    Freq_table <- count(LA,vars=c("score","grade"))
    colnames(Freq_table) <- c("scor","gra","Frequency")
  • 方法二
    library(dplyr)
    rename(count(LA,score ,grade), Freq = n)
  • 方法三
    library(reshape2)
    df_counts <- melt(table(LA))
    names(df_counts) <- names(LA)
    colnames(df_counts)[ncol(df_counts)] <- "count"
  • 方法四
    library(data.table) /##数据框转为列连表
    setDT(LA)
    ## 转化
    df[,.N,by=c('score','grade')]

blogroll

social