简体   繁体   English

R-colMeans错误(wind.speed,na.rm = T):“ x”必须为数字

[英]R - Error in colMeans(wind.speed, na.rm = T) : 'x' must be numeric

I am trying to importa single column of a text file data set where each file is a single day of data. 我正在尝试导入文本文件数据集的单个列,其中每个文件都是一天的数据。 I want to take the mean of each day's wind speed. 我想将每天的风速取平均值。 Here is the code I have written for that: 这是我为此编写的代码:

    daily.wind.speed <- c()

    file.names <- dir("C:\\Users\\User Name\\Desktop\\R project\\Sightings Data\\Weather Data", pattern =".txt")

    for(i in 1:length(file.names))
      {
##import data file into data frame
    weather.data <-read.delim(file.names[i])

## extract wind speed column
    wind.speed <- weather.data[3]

##Attempt to fix numeric error
    ##wind.speed.num <- as.numeric(wind.speed)

##Take the column mean of wind speed
    daily.avg <- colMeans(wind.speed,na.rm=T)

##Add daily average to list
    daily.wind.speed <- c(daily.wind.speed,daily.avg)

##Print for troubleshooting and progress
    print(daily.wind.speed)

    }

This code seems to work on some files in my data set, but others give me this error during this section of the code: 这段代码似乎可以在我的数据集中的某些文件上运行,但是在代码的这一部分,其他代码却给了我这个错误:

> daily.avg <- colMeans(wind.speed,na.rm=T)
Error in colMeans(wind.speed, na.rm = T) : 'x' must be numeric

I am also having trouble converting these values to numeric and am looking for options to either convert my data to numeric, or to possibly take the mean in a different way that dosen't encounter this issue. 我也很难将这些值转换为数字,并且正在寻找将数据转换为数字的选项,或者可能会以没有遇到此问题的不同方式获取均值。

> as.numeric(wind.speed.df)
Error: (list) object cannot be coerced to type 'double'

weather.data Example weather.data示例

Even though this is not a reproducible example the problem is that you are applying a matrix function to a vector so it won't work. 即使这不是一个可重现的示例,问题仍然是您正在将矩阵函数应用于矢量,因此它将不起作用。 Just change the colMeans for mean 只需将colMeans更改为mean

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 colMeans(x, na.rm = TRUE) 错误:'x' 必须是数字 - Error in colMeans(x, na.rm = TRUE) : 'x' must be numeric RDA,colMeans中的错误(x,na.rm = TRUE):当数据是数字时,&#39;x&#39;必须是数字? - RDA, Error in colMeans(x, na.rm = TRUE) : 'x' must be numeric, when data is numeric? prcomp,colMeans(x,na.rm = TRUE)错误:当我的数据为数字时,“ x”必须为数字? - prcomp, Error in colMeans(x, na.rm = TRUE) : 'x' must be numeric, when my data is numeric? colMeans中的错误(x,na.rm = TRUE):KNN分类中的&#39;x&#39;必须是数字 - Error in colMeans(x, na.rm = TRUE) : 'x' must be numeric in KNN classification 主成分分析:colMeans(x, na.rm = TRUE) 中的错误:&#39;x&#39; 必须是数字 - Principal Components Analysis:Error in colMeans(x, na.rm = TRUE) : 'x' must be numeric colMeans(x, na.rm = TRUE) 中的错误:“x”即使在将它们转换为数值变量后也必须是数值 - Error in colMeans(x, na.rm = TRUE) : 'x' must be numeric even after convert them to numerical variables prcomp“colMeans中的错误(x,na.rm = TRUE):&#39;x&#39;必须是数字” - prcomp “Error in colMeans(x, na.rm = TRUE) : 'x' must be numeric” 聚类分析:colMeans(x, na.rm = TRUE) 错误:'x' 必须是数字 - Cluster analysis: Error in colMeans(x, na.rm = TRUE) : 'x' must be numeric 为什么我在 colMeans(x, na.rm = TRUE) 中出现错误:&#39;x&#39; 必须是数字 - Why am I getting Error in colMeans(x, na.rm = TRUE) : 'x' must be numeric rStudio:colMeans(x,na.rm = TRUE)中的群集错误:“ x”必须为数字 - rStudio: Clustering Error in colMeans(x, na.rm = TRUE) : 'x' must be numeric
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM