In a matrix I need to subtract rows in the following way: row1 minus each remaining row; then row2 minus each remaining row. I need to do this operation for every single row in a matrix.
I am having three problems. First, while I was able to write a for loop for row1 minus each remaining row and print the results, I am not sure how to continue the loop for moving on to row2 minus the remaining rows etc to the last row minus remaining rows, as writing loops for each row seems unnecessary.
Problem two, while performing following subtraction e.g. row2 minus the remaining rows, I need to skip subtracting row2 from itself while running the loop. When I tried writing a for loop for row2 minus remaining rows, the results printed always show a line where row2 is subtracted from itself as well. I cannot figure out how to avoid this.
Problem three, when subtracting rows, for example, row2 minus row1; row2 minus row 3; row 2 minus row 4 etc, I want to print summary: if for each subtraction the difference between every two rows is zero. I included the if statement into the code below and it does the job, but I only managed to do it for comparing a single row versus remaining rows, so I would like to know how to apply this for each following row that should be compared against remaining rows.
Thank you in advance
library(dplyr)
# Simulate matrix of integers
set.seed(1)
df <- matrix(sample.int(5, size = 3*5, replace = TRUE), nrow = 3, ncol = 5)
print(df)
df <- tbl_df(df) # tabulate as dataframe
# For Loop for row 1
for(i in 2:nrow(df)){
result = df[1,] - df[i,]
print(result)
}
# For Loop for row 2
for(i in 1:nrow(df)){
result = df[2,] - df[i,]
print(result)
}
# Trying to print results only for those pairs of rows, between which the
difference is zero
for(i in 1:nrow(df)){
result = df[2,] - df[i,]
if (rowSums(result) == 0){
print("Duplicates present")
} else {
print("No duplicates")
}
}
Aucun commentaire:
Enregistrer un commentaire