Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/81.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在R中计算文件中的像素数?_R - Fatal编程技术网

如何在R中计算文件中的像素数?

如何在R中计算文件中的像素数?,r,R,每当cus中的值介于0-1之间时,计算cor中相应的平均值并返回结果,对2-3,3-4,5-6,7-8执行相同的操作。没有数据值被指定为NA 1-要读取corr: conne <- file("C:\\corr.bin","rb") corr <- readBin(conne, numeric(), size=4, n=1440*720, signed=TRUE) #please assume a matrix of 720*1440 dimnsions 使用数据表如何。我假设cu

每当
cus
中的值介于0-1之间时,计算
cor
中相应的平均值并返回结果,对2-3,3-4,5-6,7-8执行相同的操作。没有数据值被指定为NA

1-要读取
corr

conne <- file("C:\\corr.bin","rb")
corr <- readBin(conne, numeric(), size=4, n=1440*720, signed=TRUE)
#please assume a matrix of 720*1440 dimnsions

使用
数据表如何。我假设
cus
中的值实际上是1:7(因此基本上不确定为什么需要
cusBreak
)。我们创建一个变量来计算用于查找每组
平均值的像素数(如果数据中有NAs,请不要忘记包括
na.rm
):

require(data.table)

DT@ZadSim,如果
corr
文件包含
NA
put
0
,否则放入
1
。然后,当我们来做
求和
以了解在
平均值
中计算了多少像素时,NA单元不计入和,因为它们是0。你看到了吗?@ZadSim然后只使用
as.integer(cusBreak)
而不是
cus
来生成
数据表
,即
DT@ZadSim no,保留corr原样,因为你需要这些值的平均值。cusBREAK只是一个用于子集corr的分类变量。
conne1 <- file("C:\\use.bin","rb")
cus <- readBin(conne1, numeric(), size=4, n=1440*720, signed=TRUE)
#please assume a matrix of 720*1440 dimnsions
require( data.table )
DT <- data.table( "Cus" = as.vector(cus) , "Corr" = as.vector(corr) )
DT[ , Pix:=( ifelse( is.na( DT$Corr ) , 0 , 1 ) ) ]
DT[ , list( "Mean" = mean(Corr,na.rm=TRUE) , "Sum" = sum(Pix) ) , by = Cus ]
#  Data
set.seed(12345)
corr <- matrix( rnorm(16) , 4 )
cus <- matrix( sample(0:7,16,repl=T) , 4 )
cus
#    [,1] [,2] [,3] [,4]
#[1,]    1    6    6    2
#[2,]    5    7    3    2
#[3,]    2    4    7    0
#[4,]    2    1    6    0

#  Create data.table
DT <- data.table( "Cus" = as.vector(cus) , "Corr" = as.vector(corr) )

#  Order by Cus
setkey(DT,Cus)

# Variable to count pixels
DT[ , Pix:=( ifelse( is.na( DT$Corr ) , 0 , 1 ) ) ]

# Get meean of corr grouped by cus
DT[ ,  list( "Mean" = mean(Corr , na.rm = TRUE ) , "Sum" = sum(Pix) )  , by = Cus ]
#   Cus        Mean Pixels
#1:   1  0.15467236      2
#2:   5  0.70946602      1
#3:   2  0.08201096      4
#4:   6  0.71301325      3
#5:   7 -0.96710189      2
#6:   4  0.63009855      1
#7:   3 -0.91932200      1
#8:   0  0.03318392      2