Hi,
I just want to create four new columns which are the function of rows and columns. For example, the value of cell ‘k’ = (row ‘j’ * column ‘i’)/row total. I was trying to loop over rows and columns using the following for loop. However, it does not work.
for (j in 1:nrow(df)) {
for (i in 1:ncol(df)) {
df[j,i] <- (rowSums(df[j,])*colSums(df[,i]))/nrow(df)
}
}
A | B | C | D |
---|---|---|---|
1 | 0 | 0 | 1 |
1 | 0 | 0 | 1 |
0 | 1 | 0 | 1 |
0 | 1 | 1 | 0 |
1 | 0 | 1 | 0 |
1 | 0 | 0 | 0 |
0 | 1 | 0 | 1 |
1 | 1 | 1 | 0 |
0 | 0 | 1 | 1 |
1 | 0 | 1 | 1 |
Any help is appreciated.