我有一个整数变量。通常,转换为数字后,我可以计算出平均值。这次我使用此代码进行转换,但仍然无法正常工作。 有谁知道这是什么问题?谢谢
变量NPVC_m1的值示例如下:
771] 52524847.0 52524847.0 52524847.0 52524847.0 52524847.0 52524847.0 52524847.0 52524847.0 52524847.0 52524847.0
[781] 70026530.6 70026530.6 70026530.6 70026530.6 70026530.6 70026530.6 70026530.6 70026530.6 70026530.6 70026530.6
[791] 87524914.7 87524914.7 87524914.7 87524914.7 87524914.7 87524914.7 87524914.7 87524914.7 87524914.7 87524914.7
[801] 46418440.1 46418440.1 46418440.1 46418440.1 46418440.1 46418440.1 46418440.1 46418440.1 46418440.1 46418440.1
[811] 92833975.5 92833975.5 92833975.5 92833975.5 92833975.5 92833975.5 92833975.5 92833975.5 92833975.5 92833975.5
[821] 139000000.0 139000000.0 139000000.0 139000000.0 139000000.0 139000000.0 139000000.0 139000000.0 139000000.0 139000000.0
[831] 186000000.0 186000000.0 186000000.0 186000000.0 186000000.0 186000000.0 186000000.0 186000000.0 186000000.0 186000000.0
[841] 232000000.0 232000000.0 232000000.0
typeof(NPVC_m1)
[1] "integer"
> NPVC_m1 <- as.numeric(as.character(NPVC_m1))
> typeof(NPVC_m1)
[1] "double"
> meanNPV <- aggregate(NPVC_m1 ~ Region + Model, subsetfinal, mean)
Warning messages:
1: In mean.default(X[[i]], ...) :
argument is not numeric or logical: returning NA
2: In mean.default(X[[i]], ...) :
argument is not numeric or logical: returning NA
3: In mean.default(X[[i]], ...) :
argument is not numeric or logical: returning NA
答案 0 :(得分:1)
我不明白您在问题中显示的数字如何可以是整数:
> x <- c(52524847.0, 52524847.0, 52524847.0, 52524847.0, 52524847.0, 52524847.0,
+ 52524847.0, 52524847.0, 52524847.0, 52524847.0, 70026530.6, 70026530.6,
+ 70026530.6, 70026530.6, 70026530.6, 70026530.6, 70026530.6, 70026530.6,
+ 70026530.6, 70026530.6, 87524914.7, 87524914.7, 87524914.7, 87524914.7,
+ 87524914.7, 87524914.7, 87524914.7, 87524914.7, 87524914.7, 87524914.7,
+ 46418440.1, 46418440.1, 46418440.1, 46418440.1, 46418440.1, 46418440.1,
+ 46418440.1, 46418440.1, 46418440.1, 46418440.1, 92833975.5, 92833975.5,
+ 92833975.5, 92833975.5, 92833975.5, 92833975.5, 92833975.5, 92833975.5,
+ 92833975.5, 92833975.5, 139000000.0, 139000000.0, 139000000.0,
+ 139000000.0, 139000000.0, 139000000.0, 139000000.0, 139000000.0,
+ 139000000.0, 139000000.0, 186000000.0, 186000000.0, 186000000.0,
+ 186000000.0, 186000000.0, 186000000.0, 186000000.0, 186000000.0,
+ 186000000.0, 186000000.0, 232000000.0, 232000000.0, 232000000.0)
> as.integer(x)
[1] 52524847 52524847 52524847 52524847 52524847 52524847 52524847 52524847
[9] 52524847 52524847 70026530 70026530 70026530 70026530 70026530 70026530
[17] 70026530 70026530 70026530 70026530 87524914 87524914 87524914 87524914
[25] 87524914 87524914 87524914 87524914 87524914 87524914 46418440 46418440
[33] 46418440 46418440 46418440 46418440 46418440 46418440 46418440 46418440
[41] 92833975 92833975 92833975 92833975 92833975 92833975 92833975 92833975
[49] 92833975 92833975 139000000 139000000 139000000 139000000 139000000 139000000
[57] 139000000 139000000 139000000 139000000 186000000 186000000 186000000 186000000
[65] 186000000 186000000 186000000 186000000 186000000 186000000 232000000 232000000
[73] 232000000
如您所见,将数字强制为整数截断小数位。
请注意,as.integer()
不会不四舍五入数字:
> 70026530.6
[1] 70026531
> as.integer(70026530.6)
[1] 70026530
> options(digits=10) # to force display of decimal places
> 70026530.6
[1] 70026530.6
> as.integer(70026530.6)
[1] 70026530
此外,我可以很容易地计算出整数的平均值:
> x <- c(4, 5, 6, 6)
> typeof(x)
[1] "double"
> x <- as.integer(x)
> typeof(x)
[1] "integer"
> mean(x)
[1] 5.25
我也可以aggregate()
的方式:
> subsetfinal <- data.frame(NPVC_m1 = x,
Region = rep(c("A", "B"), c(36, 37)),
Model = c(rep(c("one", "two", "three"), 24), "one"))
> aggregate(NPVC_m1 ~ Region + Model, subsetfinal, mean)
Region Model NPVC_m1
1 A one 64632550.34
2 B one 135718369.75
3 A three 67549222.65
4 B three 139243363.88
5 A two 66091023.98
6 B two 135396195.18
因此,您遇到的问题必须与问题中显示的数字的类型或类别无关。
请创建示例数据,以使我们能够重现您的问题并将其包含在您的问题中。
typeof(NPVC_m1)
返回"integer"
或"double"
。但是 class(subsetfinal$NPVC_m1)
返回什么?正如@Roland指出的那样(请参阅下面的评论),您的变量可能是一个因素。