I have a dataframe where I have different month, I want to do a mean of each row between the month of begining (in the begin variable) and the end month (in the end variable)
nom <- letters[1:5]
pseudo <- paste(nom, 21:25, sep = "")
janvier <- c(0, 1, 1, 1, 0)
fevrier <- c(1, 1, 1, 1, 1)
mars <- c(0, 0, 0, 1, 1)
avril <- c(1, 1, 1, 0, 1)
mai <- c(1, 0, 1, 1, 1)
juin <- c(1, 1, 0, 1, 0)
df <- data.frame(nom =nom, pseudo = pseudo, janvier = janvier,
fevrier = fevrier, mars = mars, avril = avril,
mai = mai, juin = juin)
dfm <- as.matrix(df[, -c(1, 2)])
my_matrix <- matrix(nrow = 10, ncol = 6)
my_matrix <- matrix("no info", nrow = 5, ncol = 2)
colnames(my_matrix) <- c("begin", "end")
for(i in 1:dim(dfm)[1]){
for(j in 1:(dim(dfm)[2]-2)){
if(dfm[i, j] + dfm[i, j+1] + dfm[i, j+2] == 3){
my_matrix[i, 1] <- colnames(dfm)[j]
my_matrix[i, 2] <- colnames(dfm)[j+2]
break
}
}
}
output <- cbind(df, my_matrix)
output %>%
filter(begin != "no info") -> output
I tried do do it with a vectorized method, something like :
output$mean <- rowMeans(output[, output$begin:output$end])
I also tried this but not seems to recognize my begin variable :
for(i in seq_len(nrow(output))){
for(j in seq_len(ncol(output))){
output$mean[i, j] <- rowMeans(as.character(begin[i, j]):as.character(end[i, j]))
}
}
I dont want to use a loop if possible just with dplyr package, thanks for help
EDIT : I dont want to group_by, my question is a little bit complicated because I have to do means of row between the variables stored in begin and end variables