简体   繁体   中英

Multiply values from two dataframes and append in a dataframe

i have the following dataframes:

df1
 factory DC       `tariff springtouw`
  <chr>   <chr>                  <dbl>
1 Poznan  Bismarck                3000
2 Poznan  Houston                 3000
3 Poznan  Memphis                 2400
4 Poznan  Albany                  1000
5 Poznan  Seattle                 3500
df2
            Som_Product pallets kosten
duikscooter      379312   37932       
yoga              75651     226       
springtouw      1162413    9687   #    
stoel           1300512    3252       
blender         1400148    2917       

I want to multiply 3500 of df1 with 9687 of df2 and append the value in df2 on the spot of the #.

a code like this: df2$kosten[springtouw,] <- df1$tariff springtouw[5,3] * df2$pallets[3,2]

Possible output

output
df2
            Som_Product pallets kosten
duikscooter      379312   37932       
yoga              75651     226       
springtouw      1162413    9687 33904500   
stoel           1300512    3252       
blender         1400148    2917  

The rhs should be a row/column index or attribute and as we are extracting the column as a vector , it doesn't have row/column attribute ie it is just a 1 dimensional vector which can be indexed with a single integer value to extract that element at the position specified by index

df2['springtouw', 'kosten'] <- df1$`tariff springtouw`[5] * df2$pallets[3]

-output

df2
#            Som_Product pallets   kosten
#duikscooter      379312   37932         
#yoga              75651     226         
#springtouw      1162413    9687 33904500
#stoel           1300512    3252         
#blender         1400148    2917      

data

df1 <- structure(list(factory = c("Poznan", "Poznan", "Poznan", "Poznan", 
"Poznan"), DC = c("Bismarck", "Houston", "Memphis", "Albany", 
"Seattle"), `tariff springtouw` = c(3000L, 3000L, 2400L, 1000L, 
3500L)), class = "data.frame", row.names = c("1", "2", "3", "4", 
"5"))

df2 <- structure(list(Som_Product = c(379312L, 75651L, 1162413L, 1300512L, 
1400148L), pallets = c(37932L, 226L, 9687L, 3252L, 2917L), kosten = c("", 
"", "", "", "")), row.names = c("duikscooter", "yoga", "springtouw", 
"stoel", "blender"), class = "data.frame")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM