[英]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'
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.