我有以下数据框:
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
我想将 df1 的 3500 乘以 df2 的 9687,并将 df2 中的值附加到 # 处。
这样的代码: df2$kosten[springtouw,] <- df1$tariff springtouw[5,3] * df2$pallets[3,2]
可能的输出
output
df2
Som_Product pallets kosten
duikscooter 379312 37932
yoga 75651 226
springtouw 1162413 9687 33904500
stoel 1300512 3252
blender 1400148 2917
答案 0 :(得分:1)
rhs
应该是行/列索引或属性,当我们将列提取为 vector
时,它没有行/列属性,即它只是一个一维向量可以使用单个整数值进行索引以在索引指定的位置提取该元素
df2['springtouw', 'kosten'] <- df1$`tariff springtouw`[5] * df2$pallets[3]
-输出
df2
# Som_Product pallets kosten
#duikscooter 379312 37932
#yoga 75651 226
#springtouw 1162413 9687 33904500
#stoel 1300512 3252
#blender 1400148 2917
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")