简体   繁体   中英

How to calculate correlation between two variables with different sample size

a <- c(1,2,3)
b <- c(1,2)
corr <- cor(a,b)

I have two time series variables and want to compute the correlation, but they have different sample sizes. To simplify my problem, consider if there are two variables a , b and I want to calculate correlation between a and b but I only want to the first two values. How do I achieve this in R?

If you're sure that the starting point of both time series is the same (and there are no skipped values), then

n <- min(length(a),length(b))
cor(a[seq(n)],b[seq(n)])

should work to truncate both variables to the length of the shorter one.

You could subset the larger vector

a<-c(1,2,3)
b<-c(1,2)

cor(a[1:2],b) ##Using a[1:2], you are selecting only the first two vectors

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM