通过关注其他变量的范围来创建新变量
Creating new variable by focusing range of other variables
我想创建一个变量,其中包含 (startyear) 和 (endyear - 1) 之间的所有数字。我的数据如下所示:
country
leader
startyear
endyear
US
Eisenhower
1953
1961
US
Kennedy
1961
1963
我想这样显示我的数据:
country
leader
startyear
endyear
year
US
Eisenhower
1953
1961
1953
US
Eisenhower
1953
1961
1954
US
Eisenhower
1953
1961
1955
US
Eisenhower
1953
1961
1956
US
Eisenhower
1953
1961
1957
US
Eisenhower
1953
1961
1958
US
Eisenhower
1953
1961
1959
US
Eisenhower
1953
1961
1960
US
Kennedy
1961
1963
1961
US
Kennedy
1961
1963
1962
我的数据集中有很多国家。我想用“the”代码操作所有数据集。
我们可以按行和 unnest
list
列
得到序列 (:
)
library(dplyr)
library(purrr)
library(tidyr)
df1 %>%
mutate(year = map2(startyear, endyear-1, `:`)) %>%
unnest(year)
-输出
# A tibble: 10 × 5
country leader startyear endyear year
<chr> <chr> <int> <int> <int>
1 US Eisenhower 1953 1961 1953
2 US Eisenhower 1953 1961 1954
3 US Eisenhower 1953 1961 1955
4 US Eisenhower 1953 1961 1956
5 US Eisenhower 1953 1961 1957
6 US Eisenhower 1953 1961 1958
7 US Eisenhower 1953 1961 1959
8 US Eisenhower 1953 1961 1960
9 US Kennedy 1961 1963 1961
10 US Kennedy 1961 1963 1962
数据
df1 <- structure(list(country = c("US", "US"), leader = c("Eisenhower",
"Kennedy"), startyear = c(1953L, 1961L), endyear = c(1961L, 1963L
)), class = "data.frame", row.names = c(NA, -2L))
我想创建一个变量,其中包含 (startyear) 和 (endyear - 1) 之间的所有数字。我的数据如下所示:
country | leader | startyear | endyear |
---|---|---|---|
US | Eisenhower | 1953 | 1961 |
US | Kennedy | 1961 | 1963 |
我想这样显示我的数据:
country | leader | startyear | endyear | year |
---|---|---|---|---|
US | Eisenhower | 1953 | 1961 | 1953 |
US | Eisenhower | 1953 | 1961 | 1954 |
US | Eisenhower | 1953 | 1961 | 1955 |
US | Eisenhower | 1953 | 1961 | 1956 |
US | Eisenhower | 1953 | 1961 | 1957 |
US | Eisenhower | 1953 | 1961 | 1958 |
US | Eisenhower | 1953 | 1961 | 1959 |
US | Eisenhower | 1953 | 1961 | 1960 |
US | Kennedy | 1961 | 1963 | 1961 |
US | Kennedy | 1961 | 1963 | 1962 |
我的数据集中有很多国家。我想用“the”代码操作所有数据集。
我们可以按行和 unnest
list
列
:
)
library(dplyr)
library(purrr)
library(tidyr)
df1 %>%
mutate(year = map2(startyear, endyear-1, `:`)) %>%
unnest(year)
-输出
# A tibble: 10 × 5
country leader startyear endyear year
<chr> <chr> <int> <int> <int>
1 US Eisenhower 1953 1961 1953
2 US Eisenhower 1953 1961 1954
3 US Eisenhower 1953 1961 1955
4 US Eisenhower 1953 1961 1956
5 US Eisenhower 1953 1961 1957
6 US Eisenhower 1953 1961 1958
7 US Eisenhower 1953 1961 1959
8 US Eisenhower 1953 1961 1960
9 US Kennedy 1961 1963 1961
10 US Kennedy 1961 1963 1962
数据
df1 <- structure(list(country = c("US", "US"), leader = c("Eisenhower",
"Kennedy"), startyear = c(1953L, 1961L), endyear = c(1961L, 1963L
)), class = "data.frame", row.names = c(NA, -2L))