Error in apply(df$var1, 2, mean) : dim(X) must have a positive length

Error in apply(df$var1, 2, mean) : dim(X) must have a positive length

目录

Error in apply(df$var1, 2, mean) : dim(X) must have a positive length

问题:

解决:

完整错误:

Error in apply(df$var1, 2, mean) : dim(X) must have a positive length

问题:

出现错误是因为apply()函数必须应用于dataframe或matrix,我们尝试将其应用于数据帧中的特定列则发生错误。

#create data frame
df <- data.frame(points="c(99," 97, 104, 79, 84, 88, 91, 99), rebounds="c(34," 40, 41, 38, 29, 30, 22, 25), blocks="c(12," 8, 7, 11, 6, 7)) #view data frame df #attempt to calculate mean of 'points' column apply(df$points, 2, mean)< code></->

解决:

计算所有列的均值

#calculate mean of every column in data frame
apply(df, 2, mean)

  points rebounds   blocks
  92.625   32.375    8.375

计算部分列的均值

#calculate mean of 'points' and 'blocks' column in data frame
apply(df[c('points', 'blocks')], 2, mean)

points blocks
92.625  8.375

计算其中一列的均值

#calculate mean of 'points' column
mean(df$points)

[1] 92.625

完整错误:

create data frame

df

Original: https://blog.csdn.net/zhongkeyuanchongqing/article/details/120626262
Author: Data+Science+Insight
Title: Error in apply(df$var1, 2, mean) : dim(X) must have a positive length

原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/639601/

转载文章受原作者版权保护。转载请注明原作者出处!

(0)

大家都在看

亲爱的 Coder【最近整理,可免费获取】👉 最新必读书单  | 👏 面试题下载  | 🌎 免费的AI知识星球