How to multiply columns of same names belonging to

2020-08-01 07:00发布

问题:

I am having a problem... I have two data. frames with a lot of columns and these two data.frames are of different length, in fact one has many rows and second data.frame has only one row.... But in both data frames there are columns of same names. Now, I want to multiply the matching columns with each other. I fail to solve it. Please help me.

回答1:

The command

mapply("*", DataFrame1, DataFrame2)

should work if you want to multiply all columns. If the relevant columns are only a subset of all columns in the data frames, we first need to identify the columns being present in both data frames.

mapply("*", DataFrame1[intersect(names(DataFrame1), names(DataFrame2))],
       DataFrame2[intersect(names(DataFrame1), names(DataFrame2))])


标签: r