[英]Adding values in a new column based on column name of other dataset
Currently I have two datasets.目前我有两个数据集。 One with locations within the company and the amount of products that are being produced at each location on each day.
一个包含公司内的位置以及每天在每个位置生产的产品数量。 The other with the locations and the capacity of each location on each day.
另一个是每天的地点和每个地点的容量。 I have illustrated both of them below.
我在下面说明了它们。
Dataset 1 (Amount of products product on certain days of the week):数据集 1(一周中某些天的产品数量):
Location![]() |
Day of the week![]() |
Produced![]() |
---|---|---|
Location A![]() |
Monday![]() |
20 ![]() |
Location B![]() |
Monday![]() |
30 ![]() |
Location C![]() |
Monday![]() |
55 ![]() |
Location A![]() |
Tuesday![]() |
25 ![]() |
Location B![]() |
Tuesday![]() |
24 ![]() |
Location C![]() |
Tuesday![]() |
70 ![]() |
Dataset 2 (The capacity of each location on each day of the week)数据集 2(一周中每一天每个位置的容量)
Location![]() |
Monday![]() |
Tuesday![]() |
---|---|---|
Location A![]() |
50 ![]() |
50 ![]() |
Location B![]() |
60 ![]() |
50 ![]() |
Location C![]() |
80 ![]() |
80 ![]() |
What I want to do is add a new column to the first dataset.我想要做的是向第一个数据集添加一个新列。 I want that column to be named 'Capacity' and I want it to kinda look like this:
我希望将该列命名为“容量”,并且我希望它看起来像这样:
Location![]() |
Day of the week![]() |
Produced![]() |
Capacity![]() |
---|---|---|---|
Location A![]() |
Monday![]() |
20 ![]() |
50 ![]() |
Location A![]() |
Tuesday![]() |
25 ![]() |
50 ![]() |
I've tried multiple ifelse statements, but failed miserably and are working on it for quite some time now.我已经尝试了多个 ifelse 语句,但惨遭失败,并且现在正在研究它很长一段时间。 Is there someone who might know a fix?
有人可能知道修复方法吗?
You can pivot_longer
df2 and then merge
df1 and df2您可以
pivot_longer
df2然后merge
df1和df2
library(tidyr)
merge(df1,
pivot_longer(df2, colnames(df2[,2:3]),
names_to="Day of the week", values_to="Capacity"),
by=c("Location","Day of the week"))
Location Day of the week Produced Capacity
1 Location A Monday 20 50
2 Location A Tuesday 25 50
3 Location B Monday 30 60
4 Location B Tuesday 24 50
5 Location C Monday 55 80
6 Location C Tuesday 70 80
df1 <- structure(list(Location = c("Location A", "Location B", "Location C",
"Location A", "Location B", "Location C"), `Day of the week` = c("Monday",
"Monday", "Monday", "Tuesday", "Tuesday", "Tuesday"), Produced = c(20L,
30L, 55L, 25L, 24L, 70L)), class = "data.frame", row.names = c(NA,
-6L))
df2 <- structure(list(Location = c("Location A", "Location B", "Location C"
), Monday = c(50L, 60L, 80L), Tuesday = c(50L, 50L, 80L)), class = "data.frame", row.names = c(NA,
-3L))
You need to put your Capacity data into long format and then merge.您需要将容量数据放入长格式,然后合并。 Here I use
tidyr::pivot_longer()
for the former and then dplyr::left_join()
for the latter.在这里,我对前者使用
tidyr::pivot_longer()
,然后对后者使用 dplyr::left_join( dplyr::left_join()
。
library(tidyverse)
d1 <- structure(list(Location = c("Location A", "Location B", "Location C", "Location A", "Location B", "Location C"), Day_of_week = c("Monday", "Monday", "Monday", "Tuesday", "Tuesday", "Tuesday"), Produced = c(20L, 30L, 55L, 25L, 24L, 70L)), class = "data.frame", row.names = c(NA, -6L))
d2 <- structure(list(Location = c("Location A", "Location B", "Location C"), Monday = c(50L, 60L, 80L), Tuesday = c(50L, 50L, 80L)), class = "data.frame", row.names = c(NA, -3L))
d2 %>%
pivot_longer(-Location, names_to = "Day_of_week", values_to = "Capacity") %>%
left_join(d1, .)
#> Joining, by = c("Location", "Day_of_week")
#> Location Day_of_week Produced Capacity
#> 1 Location A Monday 20 50
#> 2 Location B Monday 30 60
#> 3 Location C Monday 55 80
#> 4 Location A Tuesday 25 50
#> 5 Location B Tuesday 24 50
#> 6 Location C Tuesday 70 80
Created on 2022-02-17 by the reprex package (v2.0.1)由reprex package (v2.0.1) 创建于 2022-02-17
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.