0

我正在尝试将我的数据设置为在 R 中的 mlgit 包中工作

我有一个使用以下代码创建的数据框:

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,这将是“选择”向量的第一个元素。对于 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"

我已经尝试了很多次,但无法弄清楚,也无法在网上找到相关答案。

提前致谢 :)

4

3 回答 3

0

我们可以split通过 'id' 得到 'site',并Map通过与 'choice' 中的对应值进行比较来得到一个逻辑索引。

df$indicator <- +(unlist(Map(`==`, split(df$site, df$id), choice), use.names=FALSE))

或者用 获取 'id' 的频率tabulate,复制 'choice',与 'site' 比较并转换为二进制

+(rep(choice,tabulate(df$id))==df$site)

数据

df <- data.frame(id, site)
于 2016-04-07T12:14:11.373 回答
0

正如 Akrun 建议的那样,用于data.frame定义 df:

df <- data.frame(id, site)

然后做:

df$indicator <- (df$site == choice[df$id])*1

*1T/F 结果转换为 1 和 0

结果:

   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在要转换的列上使用字符串而不是数字或因子

于 2016-04-07T13:09:53.030 回答
0

使用给定的矩阵 (df) ,指标可以计算为:

indicator <- as.numeric(choice[as.numeric(df[,"id"])] == df[,"site"])
# Final matrix 
df <- cbind(df,indicator)

所需矩阵:

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"
于 2016-04-07T14:02:01.683 回答