Losing row indexes during loop

I have two tables A (64 columns) and B(147 columns) with equal rows. I need to multiply every column of A separately with the full table B --> resulting in 64 tables. So A[,1] * B, A[,2] * B, A[,3] * B, etc.

I have used the following code:

results = vector(length = 64, mode = 'list')
for (i in 1:64) {
results[[i]] = B[rownames(A),]*dfA[,i]
}
names(results) <- vectorofnames

'''

The problem is that the 64 tables it creates do not have rownames. Table A and B both have rownames. How do I add these to my results?

in R you can get help on any function with the questionmark symbol in the R console

?row.names()

I have tried

row.names(results) <- vectorofrownames

after my loop, but it doesnt work. I need something inside the loop perhaps. Or it doesnt work because its a list. I am not sure.

what does results have to do with your loop ? your loop only alters an object called supply...

Sorry, i corrected my previous statement

yes, ok, so move that into the loop for each results[[i]] to have names set.

This part works fine. It names all the 64 tables. Or in Excel, it gives names to the Excel sheets. So now i have 64 sheets (with sheet names) with 64 tables ( with column names). The problem is row names.

ok, well I already mentioned row.names() as opposed to names() to you.
sorry for confusing you.

I tried this:

results= vector(length = 64, mode = 'list')
for (i in 1:64) {
results[[i]] = B[rownames(A),]*A[,i]
row.names(results[[i]]) = dataframewithrownames
}

but it doesnt give my row names in the sheets.

row.names(results[[i]]) = row.names(dataframewithrownames)

Still doesnt add the row names. I thought it would work as well.

Does this help ?



result_list <- list()

first_matrix <- matrix(1:4,nrow=2,ncol=2)
#name it
row.names(first_matrix) <- c("row_1","row_2")
colnames(first_matrix) <- c("col_1","col_2")

first_matrix

result_list[[1]] <- first_matrix*2

row.names(result_list[[1]]) <- toupper(row.names(first_matrix))
colnames(result_list[[1]]) <-toupper(colnames(first_matrix))

result_list

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.