Here's my code:
correlation <- function(dtable){
# create the column
dtable[, "correlation"] <- ""
for (row in 1:nrow(dtable)) {
#get the correlation for each row
cor = cor(dtable$wells_per_section[1:row], dtable$section_eur[1:row])
#store the value to the correlation column
correlation = cor
}
}
When I tried with my table it's returning NA, nothing added to the column, what am I missing?
CodePudding user response:
Your function is not assigning the result of the cor()
function to the correlation
columns, and it is not explicitly returning dtable
. Perhaps you can rewrite it, with some changes, as below:
correlation <- function(dtable){
# create the column
dtable[, "correlation"] <- NA
for (row in 1:nrow(dtable)) {
#get the correlation for each row
dtable[row,"correlation"] <- cor(dtable$wells_per_section[1:row], dtable$section_eur[1:row])
}
return(dtable)
}
If of interest, you can acheive the same like this:
dtable["correlation"] = sapply(1:nrow(dtable),\(i) cor(dtable[1:i,1], dtable[1:i,2]))
Update
The OP now wants to do correlation separately by groups of 16 rows.
I think the way to approach this is to making a grouping variable using sort(rep(..., length.out=..))
as below, and then apply a simple function by this group..
f <- function(w,s) {
sapply(seq_along(w), \(i) cor(w[1:i],s[1:i]))
}
dtable %>%
group_by(grp = sort(rep(1:16, length.out=250))) %>%
mutate(correlation = f(wells_per_section,section_eur))