首页 > 解决方案 > 我的累积百分比函数需要 R 帮助

问题描述

最近的更改(无论是在 R 中还是在其他地方)使我以前的工作功能停止工作。该函数旨在生成两列,告诉我百分位数是多少(请参阅df2$CumPercent调查中的给定分数(请参阅 参考资料df2$V1)。因此,我对逻辑的手动版本进行了一些更改,效果很好。当我在一个函数中应用相同的逻辑,它吐出一个错误,指出Var1找不到变量。任何想法这里可能出了什么问题?

df5 <- structure(list(MyVariable = c(4.66666666666667, 2.16666666666667, 
                                     5.66666666666667, 4.5, 5.16666666666667, 4.5, 1, 3.83333333333333, 
                                     2, 4, 2.33333333333333, 5.5, 5.66666666666667, 2.66666666666667, 
                                     5.66666666666667, 2.83333333333333, 4.33333333333333, 5.33333333333333, 
                                     5.66666666666667, 4.33333333333333, 2.33333333333333, 4.5, 3.66666666666667, 
                                     3.83333333333333, 2, 5, 2.83333333333333, 3, 4.83333333333333, 
                                     5.16666666666667, 3, 5.16666666666667, 1.33333333333333, 5.16666666666667, 
                                     2.16666666666667, 4, 3.66666666666667, 4, 3.5, 4.5, 3, 5.16666666666667, 
                                     4.83333333333333, 4.66666666666667, 3.16666666666667, 4.16666666666667, 
                                     2.83333333333333, 4.83333333333333, 2.66666666666667, 4.16666666666667, 
                                     5.16666666666667, 6.16666666666667, 1.83333333333333, 3.33333333333333, 
                                     4.5, 4.83333333333333, 5.5, 4.33333333333333, 4.33333333333333, 
                                     4.83333333333333, 2.33333333333333, 4.5, 4.16666666666667, 5.5, 
                                     4.5, 4.83333333333333, 5, 1, 4.5, 5, 2.33333333333333, 4, 3.5, 
                                     3.33333333333333, 4.66666666666667, 1.5, 5.83333333333333, 4.33333333333333, 
                                     5.16666666666667, 3.33333333333333, 4.66666666666667, 6, 4.33333333333333, 
                                     2.16666666666667, 4.16666666666667, 5.83333333333333, 3.66666666666667, 
                                     5, 5.83333333333333, 4.33333333333333, 4.33333333333333, 4.66666666666667, 
                                     4.83333333333333, 5.16666666666667, 5, 3.5, 5, 5.5, 4.66666666666667, 
                                     5.33333333333333, 5.5, 3.66666666666667, 1.83333333333333, 2.33333333333333, 
                                     5, 5.83333333333333, 4.66666666666667, 4.83333333333333, 5.83333333333333, 
                                     3.66666666666667, 3.33333333333333, 2.5, 5.33333333333333, 4.16666666666667, 
                                     4.16666666666667, 3.5, 3, 5.16666666666667, 3.66666666666667, 
                                     5.83333333333333, 4, 5.33333333333333, 6, 3.16666666666667, 2.33333333333333, 
                                     4.66666666666667, 5.66666666666667, 3.5, 4.66666666666667, 1.33333333333333, 
                                     4, 4.33333333333333, 3.5, 3.16666666666667, 5.16666666666667, 
                                     4.66666666666667, 2.83333333333333, 4, 2.5, 2.83333333333333, 
                                     4.83333333333333, 5.33333333333333, 4.5, 3.83333333333333, 4)), row.names = c(NA, 
                                                                                                                   -145L), class = "data.frame")

#Manual version of the cumulative percent logic (which works as intended)
PercentilesRaw <- data.frame(seq(from=0, to=7, by=.01)) #Create every increment of percentile as vector
colnames(PercentilesRaw)[colnames(PercentilesRaw)=="seq.from...0..to...7..by...0.01."] <- "V1" #Rename percentile column name
df <- data.frame(table(df5$MyVariable)) #Count the number of original values in the column
df[,"Var1"] <- as.numeric(as.character(df[,"Var1"])) #The table function above produces factor levels so need to convert to numeric
V1 <- df[,"Var1"] #Make a vector from the Var1 column
Frequency <- df[,"Freq"] #Make a vector from the Freq column
CumSum <- cumsum(df[,"Freq"]) #Calculate a cumulative sum from the Freq column
CumPercent <- CumSum/sum(df[,"Freq"])*100 #Calculate the cumulative percentage vector
CumPercent <- round(CumPercent,2) #Round the cumulative percentage vector to 2 dp
output <- cbind(round(V1,2), CumPercent) #Map the cumulative percent results to the V1 vector
df2 <- data.frame(output) #Convert the two columns into a df

#Now attempt to convert into a function.
cpave1 <- function(x) {
  PercentilesRaw <- data.frame(seq(from=0, to=7, by=.01)) #Create every increment of percentile as vector
  colnames(PercentilesRaw)[colnames(PercentilesRaw)=="seq.from...0..to...7..by...0.01."] <- "V1" #Rename percentile column name
  df <- data.frame(table(x)) #Count the number of original values in the column
  df[,"Var1"] <- as.numeric(as.character(df[,"Var1"])) #The table function above produces factor levels so need to convert to numeric
  V1 <- df[,"Var1"] #Make a vector from the Var1 column
  Frequency <- df[,"Freq"] #Make a vector from the Freq column
  CumSum <- cumsum(df[,"Freq"]) #Calculate a cumulative sum from the Freq column
  CumPercent <- CumSum/sum(df[,"Freq"])*100 #Calculate the cumulative percentage vector
  CumPercent <- round(CumPercent,2) #Round the cumulative percentage vector to 2 dp
  output <- cbind(round(V1,2), CumPercent) #Map the cumulative percent results to the V1 vector
  df2 <- data.frame(output) #Convert the two columns into a df
}

#Apply function to the MyVariable column.
MyVariable <- cpave1(df5$MyVariable)

标签: rfunctioncumulative-frequency

解决方案


由于错误消息表明您的数据中没有"Var1"列。该列称为x。这是您的函数的更短和更新版本,它返回相同的输出。

cpave1 <- function(x) {
  df <- type.convert(data.frame(table(x)), as.is = TRUE)
  data.frame(V1 = round(df$x ,2), 
             CumPercent = round(cumsum(df$Freq)/sum(df$Freq)*100, 2))
}
cpave1(df5$MyVariable)

#     V1 CumPercent
#1  1.00       1.38
#2  1.33       2.76
#3  1.50       3.45
#4  1.83       4.83
#5  2.00       6.21
#6  2.17       8.28
#7  2.33      12.41
#8  2.50      13.79
#...

推荐阅读