使用read.table不能将仅包含数值的txt文件读取为数值对象

时间:2019-05-20 22:50:09

标签: r

我读取了一个txt文件,其中仅包含带有read.table的数值,但是当我尝试对数据进行计算时,例如使用mean()查找平均值时,它表示该参数不是数值

dat= read.table("oldfaithful.txt", header= FALSE)
dat= as.numeric(c(dat))
avg= mean(dat)
Warning message:
In mean.default(dat) : argument is not numeric or logical: returning NA
Error: (list) object cannot be coerced to type 'double'

这是txt.file的代码段

216  
108  
200  
137  
272  
173  
282  
216  
117  
261 
110  
235  
252  
105  
282    

这是dat打印出来的样子

     V1
1   216
2   108
3   200
4   137
5   272
6   173
7   282
8   216
9   117
10  261
11  110
12  235
13  252
14  105
15  282

1 个答案:

答案 0 :(得分:0)

对不起,我可能应该将最后一条评论放入答案中。

您只需要平均数据帧中的V1列,而不是整个数据帧。

我认为代码的最后一行应该是

avg = mean(dat$V1)