我正在尝试将我的数据设置为在R
中的mlogit包中工作我使用以下代码创建了一个数据框:
id <- 1:10
id <- rep(id, each=5)
site <- c("site1", "site2", "site3", "site4", "site5")
choice <- c("site3", "site5", "site1", "site4", "site2",
"site4", "site3", "site5", "site2", "site1")
df <- cbind(id, site)
我想创建一个二进制变量,指示每个id值的站点选择。由于id变量是重复序列,因此除了“site”等效于“choice”的相关值的行之外,每行的新指示符变量必须为0。对于id == 1,这将是“choice”向量的第一个元素。对于id == 2,它将是选择向量的第二个元素,依此类推。
包含变量的最终数据框应如下所示:
id site indicator
[1,] "1" "site1" "0"
[2,] "1" "site2" "0"
[3,] "1" "site3" "1"
[4,] "1" "site4" "0"
[5,] "1" "site5" "0"
[6,] "2" "site1" "0"
[7,] "2" "site2" "0"
[8,] "2" "site3" "0"
[9,] "2" "site4" "0"
[10,] "2" "site5" "1"
[11,] "3" "site1" "1"
[12,] "3" "site2" "0"
[13,] "3" "site3" "0"
[14,] "3" "site4" "0"
[15,] "3" "site5" "0"
[16,] "4" "site1" "0"
[17,] "4" "site2" "0"
[18,] "4" "site3" "0"
[19,] "4" "site4" "1"
[20,] "4" "site5" "0"
[21,] "5" "site1" "0"
[22,] "5" "site2" "1"
[23,] "5" "site3" "0"
[24,] "5" "site4" "0"
[25,] "5" "site5" "0"
[26,] "6" "site1" "0"
[27,] "6" "site2" "0"
[28,] "6" "site3" "0"
[29,] "6" "site4" "1"
[30,] "6" "site5" "0"
[31,] "7" "site1" "0"
[32,] "7" "site2" "0"
[33,] "7" "site3" "1"
[34,] "7" "site4" "0"
[35,] "7" "site5" "0"
[36,] "8" "site1" "0"
[37,] "8" "site2" "0"
[38,] "8" "site3" "0"
[39,] "8" "site4" "0"
[40,] "8" "site5" "1"
[41,] "9" "site1" "0"
[42,] "9" "site2" "1"
[43,] "9" "site3" "0"
[44,] "9" "site4" "0"
[45,] "9" "site5" "0"
[46,] "10" "site1" "1"
[47,] "10" "site2" "0"
[48,] "10" "site3" "0"
[49,] "10" "site4" "0"
[50,] "10" "site5" "0"
我已多次尝试过,无法弄明白,也无法在线找到相关答案。
提前致谢:)
答案 0 :(得分:0)
我们可以通过'id'split
'site',并使用Map
通过与'choice'中的相应值进行比较来获取逻辑索引。
df$indicator <- +(unlist(Map(`==`, split(df$site, df$id), choice), use.names=FALSE))
或者使用tabulate
获取'id'的频率,复制'choice',与'site'比较并转换为二进制
+(rep(choice,tabulate(df$id))==df$site)
df <- data.frame(id, site)
答案 1 :(得分:0)
正如Akrun建议的那样,使用data.frame
来定义df:
df <- data.frame(id, site)
然后做:
df$indicator <- (df$site == choice[df$id])*1
*1
会将T / F结果转换为1&0 39;
结果:
id site indicator
1 1 site1 0
2 1 site2 0
3 1 site3 1
4 1 site4 0
5 1 site5 0
6 2 site1 0
7 2 site2 0
8 2 site3 0
9 2 site4 0
10 2 site5 1
11 3 site1 1
12 3 site2 0
13 3 site3 0
14 3 site4 0
15 3 site5 0
16 4 site1 0
17 4 site2 0
18 4 site3 0
19 4 site4 1
20 4 site5 0
21 5 site1 0
22 5 site2 1
23 5 site3 0
24 5 site4 0
25 5 site5 0
26 6 site1 0
27 6 site2 0
28 6 site3 0
29 6 site4 1
30 6 site5 0
31 7 site1 0
32 7 site2 0
33 7 site3 1
34 7 site4 0
35 7 site5 0
36 8 site1 0
37 8 site2 0
38 8 site3 0
39 8 site4 0
40 8 site5 1
41 9 site1 0
42 9 site2 1
43 9 site3 0
44 9 site4 0
45 9 site5 0
46 10 site1 1
47 10 site2 0
48 10 site3 0
49 10 site4 0
50 10 site5 0
如果您想要字符串而不是数字或因子,请在要转换的列上使用as.character
答案 2 :(得分:0)
使用给定矩阵(df),指标可以计算为:
indicator <- as.numeric(choice[as.numeric(df[,"id"])] == df[,"site"])
# Final matrix
df <- cbind(df,indicator)
Matrix Desired:
id site indicator
[1,] "1" "site1" "0"
[2,] "1" "site2" "0"
[3,] "1" "site3" "1"
[4,] "1" "site4" "0"
[5,] "1" "site5" "0"
[6,] "2" "site1" "0"
[7,] "2" "site2" "0"
[8,] "2" "site3" "0"
[9,] "2" "site4" "0"
[10,] "2" "site5" "1"
[11,] "3" "site1" "1"
[12,] "3" "site2" "0"
[13,] "3" "site3" "0"
[14,] "3" "site4" "0"
[15,] "3" "site5" "0"
[16,] "4" "site1" "0"
[17,] "4" "site2" "0"
[18,] "4" "site3" "0"
[19,] "4" "site4" "1"
[20,] "4" "site5" "0"
[21,] "5" "site1" "0"
[22,] "5" "site2" "1"
[23,] "5" "site3" "0"
[24,] "5" "site4" "0"
[25,] "5" "site5" "0"
[26,] "6" "site1" "0"
[27,] "6" "site2" "0"
[28,] "6" "site3" "0"
[29,] "6" "site4" "1"
[30,] "6" "site5" "0"
[31,] "7" "site1" "0"
[32,] "7" "site2" "0"
[33,] "7" "site3" "1"
[34,] "7" "site4" "0"
[35,] "7" "site5" "0"
[36,] "8" "site1" "0"
[37,] "8" "site2" "0"
[38,] "8" "site3" "0"
[39,] "8" "site4" "0"
[40,] "8" "site5" "1"
[41,] "9" "site1" "0"
[42,] "9" "site2" "1"
[43,] "9" "site3" "0"
[44,] "9" "site4" "0"
[45,] "9" "site5" "0"
[46,] "10" "site1" "1"
[47,] "10" "site2" "0"
[48,] "10" "site3" "0"
[49,] "10" "site4" "0"
[50,] "10" "site5" "0"