要在我的女性固定影响估计中包括各州的年度趋势,我想为每个州创建一个单独的变量,第一年的值为1,第二年的值为2,依此类推,对于第一年的值为0。其他所有州。 我设法创建了一个满足所有这些要求的变量(在示例中为“ stateyear”),只是它只是所有州的一个变量(请参见示例)。是否有一种方法可以将每个变量按组(state_geocode_id)拆分为多个变量,并将除相应状态以外的所有其他状态都设置为0?
简化的数据集:
<!DOCTYPE html>
<html>
<head>
<title>Rainbow</title>
<meta charset="UTF-8" />
<style>
* { box-sizing: border-box; }
.separate { width: 100%; height: 10em; }
.separate>* { width: 100%; height: 100%; margin-top: 1em; }
.overlay { width: 100%; height: 10em; filter: brightness(3); }
.overlay>* { width: 100%; height: 100%; position: absolute; }
.overlay>:nth-of-type(1) { opacity: 1; }
.overlay>:nth-of-type(2) { opacity: .5; }
.overlay>:nth-of-type(3) { opacity: .33; }
.overlay>:nth-of-type(4) { opacity: .25; }
.blue { background: linear-gradient(
90deg, rgb(0,0,256) 0%, rgb(0,0,256) 5%, rgb(0,0,0) 20% ); }
.green { background: linear-gradient(
90deg, rgb(0,0,0) 0%, rgb(0,256,0) 25%, rgb(0,256,0) 35%, rgb(0,0,0) 55% ); }
.red { background: linear-gradient(
90deg, rgb(0,0,0) 15%, rgb(256,0,0) 35%, rgb(256,0,0) 45%, rgb(0,0,0) 100% ); }
.blue2 { background: linear-gradient(
90deg, rgb(0,0,0) 65%, rgb(0,0,256) 95%, rgb(0,0,256) 100% ); }
</style>
</head>
<body>
<h1>Rainbow</h1>
<div class="overlay">
<div class="blue"></div>
<div class="green"></div>
<div class="red"></div>
<div class="blue2"></div>
</div>
<div class="separate">
<div class="blue"></div>
<div class="green"></div>
<div class="red"></div>
<div class="blue2"></div>
</div>
</body>
</html>
添加“州年”变量:
d <- data.frame("100")
names(d) <- "state_geocode_id"
d$state_geocode_id <- as.character(d$state_geocode_id)
d <- rbind(d, "100", "100", "100", "101", "101", "101", "101", "102", "102", "102", "102")
d$municip <- c("1", "1", "2", "2", "3", "3", "4", "4", "5", "5", "6", "6")
d$year <- c("2000", "2001", "2000", "2001","2000", "2001", "2000", "2001", "2000", "2001", "2000", "2001")
答案 0 :(得分:2)
喜欢吗?
library(tidyverse)
d %>%
group_by(state_geocode_id) %>%
#add a row counter and duplicate the state id
mutate(row = row_number(),
state2 = state_geocode_id) %>%
#now spread by the duplicate state to get a column for each state
spread(key = state2, value = row, fill = 0)
# A tibble: 12 x 6
# Groups: state_geocode_id [4]
state_geocode_id year `100` `101` `102` `103`
<chr> <chr> <dbl> <dbl> <dbl> <dbl>
1 100 2000 1 0 0 0
2 100 2001 2 0 0 0
3 100 2002 3 0 0 0
4 101 2000 0 1 0 0
5 101 2001 0 2 0 0
6 101 2002 0 3 0 0
7 102 2000 0 0 1 0
8 102 2001 0 0 2 0
9 102 2002 0 0 3 0
10 103 2000 0 0 0 1
11 103 2001 0 0 0 2
12 103 2002 0 0 0 3