只要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
2-阅读cus
:
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
答案 0 :(得分:1)
如何使用data.table
。我假设cus
中的值实际上是1:7(所以基本上不确定为什么需要cusBreak
)。我们创建一个变量来计算用于查找每个组的mean
的像素数(如果您的数据中有NA,则不要忘记包含na.rm
):
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