Apologize for the obscure title...
I have a column Dato
in data frame tucker_df
and another data frame jrn_not
. I am trying to add each row in tucker_df$Dato
to each row in the data frame jrn_not
in new column jrn_not$date
. The final date frame will have 147x12 rows where each Name
appear in five separate rows with five different dates in a new column date
tucker_df$Dato
[1] "2022-02-11" "2022-02-02" "2022-02-02" "2022-01-31" "2022-01-28" "2022-01-27" "2022-01-21" "2022-01-17" "2022-01-15" "2022-01-04" "2021-12-21"
[12] "2021-12-02" "2021-11-30" "2021-11-11" "2021-10-30" "2021-10-20" "2021-10-15" "2021-10-05" "2021-09-24" "2021-09-22" "2021-09-18" "2021-09-04"
[23] "2021-09-03" "2021-09-02" "2021-08-28" "2021-08-20" "2021-08-18" "2021-08-12" "2021-08-10" "2021-08-03" "2021-08-03" "2021-07-28" "2021-07-22"
[34] "2021-07-15" "2021-07-13" "2021-07-13" "2021-07-08" "2021-07-06" "2021-07-01" "2021-06-18" "2021-06-16" "2021-06-15" "2021-06-09" "2021-06-02"
[45] "2021-05-15" "2021-05-12" "2021-05-06" "2021-04-19" "2021-04-16" "2021-04-13" "2021-04-03" "2021-03-31" "2021-03-24" "2021-03-18" "2021-03-16"
[56] "2021-03-12" "2021-03-11" "2021-02-22" "2021-02-16" "2021-02-11" "2021-01-08" "2020-12-10" "2020-08-04" "2020-07-07" "2020-06-30" "2020-06-12"
[67] "2020-06-11" "2020-06-09" "2020-06-05" "2020-06-04" "2020-06-03" "2020-05-29" "2020-05-28" "2020-05-27" "2020-05-22" "2020-05-20" "2020-05-05"
[78] "2020-04-29" "2020-04-28" "2020-04-18" "2020-04-16" "2020-04-07" "2020-04-07" "2020-03-26" "2020-03-12" "2020-03-05" "2020-02-22" "2020-02-21"
[89] "2020-02-14" "2020-02-12" "2020-02-11" "2020-02-05" "2020-02-04" "2020-01-24" "2020-01-23" "2020-01-17" "2020-01-16" "2020-01-16" "2020-01-15"
[100] "2020-01-09" "2019-12-20" "2019-12-18" "2019-12-12" "2019-12-04" "2019-11-26" "2019-11-21" "2019-11-15" "2019-10-29" "2019-10-16" "2019-10-08"
[111] "2019-09-27" "2019-09-24" "2019-09-20" "2019-09-19" "2019-09-12" "2019-09-06" "2019-08-01" "2019-07-30" "2019-07-03" "2019-06-26" "2019-06-12"
[122] "2019-06-04" "2019-05-21" "2019-05-14" "2019-05-09" "2019-05-07" "2019-05-03" "2019-04-30" "2019-04-24" "2019-04-19" "2019-04-12" "2019-03-28"
[133] "2019-03-27" "2019-03-26" "2019-03-13" "2019-03-08" "2019-02-22" "2019-02-01" "2019-01-29" "2019-01-16" "2019-01-15" "2019-01-11" "2018-12-20"
[144] "2018-12-12" "2018-12-05" "2018-11-29" "2018-11-27"
jrn_not
# A tibble: 12 x 3
Name twitter_handle Follower_count
<chr> <chr> <dbl>
1 Paul Begala @PaulBegala 235900
2 Ana Cabrera @AnaCabrera 211600
3 Josh Campbell @joshscampbell 229500
4 Chris Cillizza @ChrisCillizza 642500
5 S.E. Cupp @secupp 464100
6 Hala Gorani @HalaGorani 206700
7 Dr. Sanjay Gupta @drsanjaygupta 2500000
8 Omar Jimenez @OmarJimenez 301400
9 Andrew Kaczynski @KFILE 445300
10 John King @JohnKingCNN 590900
11 Donie O'Sullivan @donie 286200
12 Jeff Zeleny @jeffzeleny 308500
Using the following code adds all dates tucker_df$Dato
to every row in jrn_not
. However, is there a way to add single dates from tucker_df$Dato
one by one so I end up with a data frame jrn_not
consisting of 147x12 row
jrn_not$date <- list(as.list(tucker_df$Dato))
jrn_not
# A tibble: 12 x 4
Name twitter_handle Follower_count date
<chr> <chr> <dbl> <list>
1 Paul Begala @PaulBegala 235900 <list [147]>
2 Ana Cabrera @AnaCabrera 211600 <list [147]>
3 Josh Campbell @joshscampbell 229500 <list [147]>
4 Chris Cillizza @ChrisCillizza 642500 <list [147]>
5 S.E. Cupp @secupp 464100 <list [147]>
6 Hala Gorani @HalaGorani 206700 <list [147]>
7 Dr. Sanjay Gupta @drsanjaygupta 2500000 <list [147]>
8 Omar Jimenez @OmarJimenez 301400 <list [147]>
9 Andrew Kaczynski @KFILE 445300 <list [147]>
10 John King @JohnKingCNN 590900 <list [147]>
11 Donie O'Sullivan @donie 286200 <list [147]>
12 Jeff Zeleny @jeffzeleny 308500 <list [147]>
CodePudding user response:
Suppose your data frame is
df <- iris[seq(10),]
# Sepal.Length Sepal.Width Petal.Length Petal.Width Species
#1 5.1 3.5 1.4 0.2 setosa
#2 4.9 3.0 1.4 0.2 setosa
#3 4.7 3.2 1.3 0.2 setosa
#4 4.6 3.1 1.5 0.2 setosa
#5 5.0 3.6 1.4 0.2 setosa
#6 5.4 3.9 1.7 0.4 setosa
#7 4.6 3.4 1.4 0.3 setosa
#8 5.0 3.4 1.5 0.2 setosa
#9 4.4 2.9 1.4 0.2 setosa
#10 4.9 3.1 1.5 0.1 setosa
And you want to add
x <- c(1, 2, 3)
Then you can replicate the values and cbind
, i.e.
cbind(df, new = rep(x, each = nrow(df)))
Sepal.Length Sepal.Width Petal.Length Petal.Width Species new
1 5.1 3.5 1.4 0.2 setosa 1
2 4.9 3.0 1.4 0.2 setosa 1
3 4.7 3.2 1.3 0.2 setosa 1
4 4.6 3.1 1.5 0.2 setosa 1
5 5.0 3.6 1.4 0.2 setosa 1
6 5.4 3.9 1.7 0.4 setosa 1
7 4.6 3.4 1.4 0.3 setosa 1
8 5.0 3.4 1.5 0.2 setosa 1
9 4.4 2.9 1.4 0.2 setosa 1
10 4.9 3.1 1.5 0.1 setosa 1
11 5.1 3.5 1.4 0.2 setosa 2
12 4.9 3.0 1.4 0.2 setosa 2
13 4.7 3.2 1.3 0.2 setosa 2
14 4.6 3.1 1.5 0.2 setosa 2
15 5.0 3.6 1.4 0.2 setosa 2
16 5.4 3.9 1.7 0.4 setosa 2
17 4.6 3.4 1.4 0.3 setosa 2
18 5.0 3.4 1.5 0.2 setosa 2
19 4.4 2.9 1.4 0.2 setosa 2
20 4.9 3.1 1.5 0.1 setosa 2
21 5.1 3.5 1.4 0.2 setosa 3
22 4.9 3.0 1.4 0.2 setosa 3
23 4.7 3.2 1.3 0.2 setosa 3
24 4.6 3.1 1.5 0.2 setosa 3
25 5.0 3.6 1.4 0.2 setosa 3
26 5.4 3.9 1.7 0.4 setosa 3
27 4.6 3.4 1.4 0.3 setosa 3
28 5.0 3.4 1.5 0.2 setosa 3
29 4.4 2.9 1.4 0.2 setosa 3
30 4.9 3.1 1.5 0.1 setosa 3