0

I have the following code:

supply = vector(length = 64, mode = 'list')
for (i in 1:64) {
  supply[[i]] = df3[rownames(df6),]*df6[,i]

names(supply) <- sheetnames

both df3 and df6 have row names, which is use to match the 64 new tables on. In these new tables the row names dissappear (column names are still there). How do I get the row names in my results? I need to export them to Excel including the row names which are matched in the for loop.

**edit

i tried the following:

supply = vector(length = 64, mode = 'list')
for (i in 1:64) {
  supply[[i]] = df3[rownames(df6),]*df6[,i]
  row.names(supply[[i]]) = row.names(df6)}

but it does not work

Red_24
  • 15
  • 5
  • Add whole data. dput(yourdata). See this for more [information](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) – Daman deep Nov 25 '20 at 10:20
  • @Damandeep The problem is that the data is quite large and because of my work confidential.. df3 has 580 rows and df6 has 374 rows. I am matching the 374 from both tables. This seems to work but it doesnt report the rownames in results. – Red_24 Nov 25 '20 at 10:34
  • how many columns does each dataframe have? – Edo Nov 25 '20 at 10:42
  • df6 has 64 columns and df3 has 147 columns. Every column of df6 is multplied by the entire table of df3 separately. Resulting in 64 tables with 147 columns and 374 rows. The 374 rows are the rows that have the same index in both tables. It works, but just doesnt report the row indexes in the 64 tables. – Red_24 Nov 25 '20 at 10:44

1 Answers1

0

You can try with this. It should return exactly what you're looking for, with the exception that rownames is a column.

# get initial columnnames
colnames3 <- names(df3)
colnames6 <- names(df6)

# set rownames as a column names "rowname"
df3 <- tibble::rownames_to_column(df3)
df6 <- tibble::rownames_to_column(df6)

# join by rowname
df3 <- dplyr::inner_join(df3, df6, by = "rowname")

# define the columns you need
out <- df3[c("rowname", colnames3)]

# your loop!
supply <- lapply(colnames6, function(col){

        out[colnames3] <- df3[colnames3] * df3[,col]
        out

})

Without a reproducible example is difficult to help you more.

lapply returns a list, so you don't need to initialize supply before and you don't need a for loop either.

Edo
  • 7,567
  • 2
  • 9
  • 19
  • This might be better to do indeed. It gives me an error though: Error in Ops.data.frame(df3[colnames3], df3[col]) : ‘*’ only defined for equally-sized data frames df3 and df6 now have the same amount of rows, but not the same amount of columns. This might be the problem? df3 has 212 columns and df6 has 65 columns still. – Red_24 Nov 25 '20 at 11:48
  • forgot a comma in df3[,col]. Try again. I've updated my answer – Edo Nov 25 '20 at 11:55
  • It does run now. But I am sorry but it gives the same results as my earlier attempt. Did you also check my new code? I think i am close. Is the problem that they are dataframes inside a list? – Red_24 Nov 25 '20 at 12:05
  • Then you need to produce a reproducible example. I can't help you more than this without it. – Edo Nov 25 '20 at 12:11
  • I just checked again, and it may have worked! when i view a specific dataframe in Rstudio of "supply" it does give me row names. However when i export it to Excel it doesnt show me row names. I use write_xlsx(supply, "filepath") – Red_24 Nov 25 '20 at 12:18
  • Thank you for your help and patience. This seems to work. Just started R 3 days ago, so i am still learning. Could you send me or comment here somewhere the previous version in which you made the rownames the index? I lost that one, but would like to try for myself later :) – Red_24 Nov 25 '20 at 12:38