Related
I have the following dataset and I'm trying to run the mediate function on it:
> str(df)
tibble [33 × 7] (S3: tbl_df/tbl/data.frame)
$ BoroughName : chr [1:33] "City of London" "Barking and Dagenham" "Barnet" "Bexley" ...
$ BoroughPopulation : num [1:33] 5.2 98.3 208.3 142.3 171.3 ...
$ PM25PWACugm3 : num [1:33] 17.9 13.2 13.2 13 13.7 12.9 15.1 13.2 13.5 13 ...
$ Baselinedeaths : num [1:33] 40 1247 2353 1814 1477 ...
$ Attributablefraction: num [1:33] 9.9 7.4 7.4 7.3 7.7 7.2 8.4 7.4 7.6 7.3 ...
$ Attributabledeaths : num [1:33] 4 92 174 132 112 182 91 173 142 138 ...
$ Lifeyearslost : num [1:33] 60 1268 2360 1772 1855 ...
I'm trying to run the mediate function with the following models for linear regression:
# fit a linear regression model to predict PM2.5 PWAC from borough BoroughPopulation
model1 <- lm(df$PM25PWACugm3 ~ df$BoroughPopulation)
# fit a linear regression model to predict baseline deaths from PM2.5 PWAC
model2 <- lm(df$Baselinedeaths ~ df$PM25PWACugm3)
# fit a linear regression model to predict baseline deaths from borough BoroughPopulation and PM2.5 PWAC
model3 <- lm(df$Baselinedeaths ~ df$BoroughPopulation + df$PM25PWACugm3)
# install and load the 'mediation' package
library(mediation)
mediate_result <- mediate(model1, model2, model3, treat = "BoroughPopulation", mediator = "PM25PWACugm3", data = df)
But I keep getting this error:
Error in `[.data.frame`(m.data, , treat) : undefined columns selected
I have checked the names of the columns multiple times and whether they contain any spaces or special characters, but they don't. The types of the variables also seem correct and I genuinely don't know what else could be the problem. Any help to make the mediate function work would be appreciated!
This is the dput(df) output:
dput(df)
structure(list(BoroughName = c("City of London", "Barking and Dagenham",
"Barnet", "Bexley", "Brent", "Bromley", "Camden", "Croydon",
"Ealing", "Enfield", "Greenwich", "Hackney", "Hammersmith and Fulham",
"Haringey", "Harrow", "Havering", "Hillingdon", "Hounslow", "Islington",
"Kensington and Chelsea", "Kingston upon Thames", "Lambeth",
"Lewisham", "Merton", "Newham", "Redbridge", "Richmond upon Thames",
"Southwark", "Sutton", "Tower Hamlets", "Waltham Forest", "Wandsworth",
"Westminster"), BoroughPopulation = c(5.2, 98.3, 208.3, 142.3,
171.3, 199.8, 125.1, 212, 194, 177.3, 141.2, 126.1, 102.5, 142.9,
142.4, 150.1, 157, 143.3, 111.9, 101.5, 95.3, 165.4, 157, 119.9,
144.5, 157.6, 120.7, 157.7, 118.7, 118.6, 141.6, 173.4, 133.6
), PM25PWACugm3 = c(17.9, 13.2, 13.2, 13, 13.7, 12.9, 15.1, 13.2,
13.5, 13, 13.6, 14.5, 14.5, 13.7, 12.8, 12.6, 12.7, 13.4, 15.3,
15.1, 13.1, 14.4, 13.9, 13.5, 14, 13.3, 13.3, 14.9, 13.1, 15.5,
13.5, 14.1, 15.5), Baselinedeaths = c(40, 1247, 2353, 1814, 1477,
2524, 1085, 2349, 1867, 1897, 1584, 1015, 882, 1106, 1386, 2126,
1754, 1362, 1035, 803, 1000, 1360, 1552, 1143, 1221, 1716, 1131,
1338, 1391, 983, 1356, 1513, 1071), Attributablefraction = c(9.9,
7.4, 7.4, 7.3, 7.7, 7.2, 8.4, 7.4, 7.6, 7.3, 7.6, 8.1, 8.1, 7.7,
7.2, 7.1, 7.1, 7.5, 8.5, 8.4, 7.4, 8.1, 7.8, 7.6, 7.9, 7.4, 7.4,
8.3, 7.3, 8.7, 7.6, 7.9, 8.6), Attributabledeaths = c(4, 92,
174, 132, 112, 182, 91, 173, 142, 138, 120, 82, 71, 85, 100,
150, 125, 102, 88, 67, 74, 109, 120, 86, 96, 128, 84, 111, 102,
85, 102, 119, 92), Lifeyearslost = c(60, 1268, 2360, 1772, 1855,
2379, 1568, 2423, 2175, 1944, 1659, 1429, 1166, 1472, 1544, 1968,
1788, 1564, 1394, 1119, 1008, 1797, 1773, 1259, 1572, 1799, 1238,
1793, 1367, 1314, 1546, 1686, 1570)), row.names = c(NA, -33L), class = c("tbl_df",
"tbl", "data.frame"))
I have the variables price (dollars) and size (sq.feet). From my dataset, I want to find the 10 houses that best fit the description "price close to 100 and size close to 30".
I can see that I have to use the abs() function, and find the smallest absolute difference. However, I want both conditions to be true at the same time, I don't know how to do that with abs.
I thought about doing
which(abs(...))
but I don't know how.
Any ideas?
The data:
data_struct <-
structure(
list(
new_baltimore.SQFT = c(
11.25,
28.92,
30.62,
26.12,
22.04,
39.42,
21.88,
25.6,
44.12,
19.88,
12.08,
10.99,
12.8,
29.79,
14.3,
13.72,
11.84,
18.06,
10.72,
8.96,
14.38,
36.75,
20,
22.82,
24.86,
19.2,
11.58,
26,
14.4,
11.62,
23.08,
23.76,
15.6,
10,
22.8,
16.76,
22.1,
14.28,
15.36,
16,
23.04,
24.94,
11.82,
12.88,
11.2,
18.12,
38.25,
17.68,
19.02,
32.8,
15.16,
21.975,
12.6,
23.52,
17.52,
47.61,
20.55,
35.52,
8.4,
13.68,
14.48,
12.8,
12.8,
18,
15.4,
10.08,
8.96,
8.96,
20,
12.88,
12,
18.16,
14.28,
26,
12.02,
20.8,
11.78,
8.68,
17.6,
11.4,
44.55,
46.32,
10.24,
9.6,
31.2,
26.4,
13.6,
27.48,
17.86,
18.04,
14.84,
10.46,
14.56,
6.96,
9.5,
11.86,
12.88,
12.32,
6.72,
10.08,
15.6,
6.72,
11.52,
11.76,
10.24,
11.52,
9.28,
6.72,
15.6,
15.5,
9.84,
15.6,
13.76,
10.24,
5.76,
10.08,
11.52,
12.15,
9.77,
15,
14.4,
14.5,
22.54,
10.24,
7.8,
8.4,
10.92,
42.9,
9,
10.5,
10.08,
12.6,
8.96,
8.58,
7.56,
10.8,
13.44,
10.24,
14.44,
12.24,
13.2,
9.6,
15.22,
24.16,
10.24,
10.24,
9.88,
23.2,
17.68,
24.3,
35.94,
21.6,
11.02,
21,
23.92,
14.4,
28,
11.44,
21.94,
10.24,
16.86,
9.92,
13.44,
12,
14.76,
8.96,
11.52,
8.64,
8.12,
11.12,
11.28,
10.36,
11.52,
17.1,
17.52,
10.73,
11.2,
12.8,
12,
41.07,
12.8,
22.36,
10.56,
13.44,
11.02,
17.98,
18.88,
11.76,
9.36,
11.52,
27.3,
23.04,
17.68,
13.36,
11.6,
11.52,
9.98,
12.96,
11.13,
19.6,
11.52,
12.16,
0,
10.64
),
new_baltimore.PRICE = c(
47,
113,
165,
104.3,
62.5,
70,
127.5,
64.5,
145,
63.5,
58.9,
65,
48,
3.5,
12.8,
17.5,
36,
41.9,
53.5,
24.5,
24.5,
55.5,
60,
51,
46,
46,
44,
54.9,
42.5,
44,
44.9,
37.9,
33,
43.9,
49.6,
52,
37.5,
50,
35.9,
42.9,
107,
112,
44.9,
55,
102,
35.5,
62.9,
39,
110,
8,
62,
85.9,
57,
110,
67.7,
89.5,
70,
74,
13,
48,
24,
53.5,
34.5,
53,
87.5,
33.5,
24,
9.6,
30,
41,
30,
38.9,
20.7,
49.9,
18.6,
39,
34,
16,
18.9,
15.2,
41.5,
53,
22,
24.9,
6.7,
32.5,
30,
59,
29.5,
26,
16.5,
39,
48.9,
33.5,
46,
54,
57.9,
37.9,
32,
31,
34,
29,
32.5,
51.9,
31,
41.8,
48,
28,
35,
46.5,
51.9,
35.4,
16,
35,
35,
36.5,
35.9,
45,
40,
35,
38,
37,
23,
25.5,
39.5,
21.5,
9,
67.5,
13.4,
12.5,
28.5,
23,
33.5,
9,
11,
30.9,
31.65,
33,
33.4,
47,
40,
46,
45.5,
57,
29.9,
30,
34,
51,
64.5,
57.5,
85.5,
61,
38,
56.5,
60.4,
51.5,
54,
69,
56,
27.9,
37.5,
32.9,
22,
29.9,
39.9,
32.6,
38.5,
21.5,
25.9,
27.5,
22.9,
31.5,
8.5,
5.5,
33,
57,
47,
43.5,
43.9,
68.5,
44.25,
61,
40,
44.5,
57,
35,
35.1,
64.5,
40,
42.6,
50,
58,
58,
55,
43,
54,
39,
45,
42,
38.9,
43.215,
26.5,
30,
29.5
)
),
class = "data.frame",
row.names = c(NA,-204L)
)
I think you're right with absolute differences. Try to subtract price and size to get a zero point, similar to de-meaning. Using replicate we subtract a respective matrix. From the absolute differences first rank the columns then again the rowSums. The first k values of the order should give you the desired appartements!
f <- \(price, size, k, data) {
aux <- abs(data - t(replicate(nrow(data), c(size, price))))
data[order(rank(rowSums(as.data.frame(lapply(aux, rank)))))[1:k], ]
}
f(price=100, size=30, k=10, data=data_struct[1:2])
# new_baltimore.SQFT new_baltimore.PRICE
# 2 28.920 113.0
# 4 26.120 104.3
# 42 24.940 112.0
# 54 23.520 110.0
# 58 35.520 74.0
# 41 23.040 107.0
# 151 35.940 85.5
# 8 25.600 64.5
# 88 27.480 59.0
# 52 21.975 85.9
You could do:
data_struct[head(order(pdist::pdist(data_struct, c(30,100))#dist),10), ]
new_baltimore.SQFT new_baltimore.PRICE
4 26.120 104.3
41 23.040 107.0
54 23.520 110.0
42 24.940 112.0
2 28.920 113.0
49 19.020 110.0
151 35.940 85.5
52 21.975 85.9
45 11.200 102.0
65 15.400 87.5
using tidyverse you could do:
data_struct %>%
mutate(price_diff = (new_baltimore.PRICE -100)^2,
size_diff = (new_baltimore.SQFT-30)^2) %>%
slice_min(price_diff + size_diff, n=10) %>%
select(-ends_with('diff'))
new_baltimore.SQFT new_baltimore.PRICE
1 26.120 104.3
2 23.040 107.0
3 23.520 110.0
4 24.940 112.0
5 28.920 113.0
6 19.020 110.0
7 35.940 85.5
8 21.975 85.9
9 11.200 102.0
10 15.400 87.5
in base R:
data_struct[head(order(colSums((t(data_struct) - c(30, 100))^2)), 10),]
new_baltimore.SQFT new_baltimore.PRICE
4 26.120 104.3
41 23.040 107.0
54 23.520 110.0
42 24.940 112.0
2 28.920 113.0
49 19.020 110.0
151 35.940 85.5
52 21.975 85.9
45 11.200 102.0
65 15.400 87.5
I want to do LDA (linear discriminant analysis) with the Auto dataset of the ISLR package. To start off, I am trying to take the cars with year = 75 and use it as a "test set", where cars of all other years will be used as a "training set". However, it seems that I've made a mess of things. For instance, in my code below, sequentially using the replace function for the values of mpg.year75 just results in everything being set to high:
Auto.df <- ISLR::Auto
train <- Auto.df$year != 75
Auto.df.notyear75 <- Auto.df[train,]
mpg.notyear75 <- Auto.df.notyear75$mpg
Auto.df.year75 <- Auto.df[!train,] # !train chooses subset complementary to train
mpg.year75 <- Auto.df.year75$mpg
Auto.df.notyear75 <- as_tibble(Auto.df.notyear75)
Auto.df.notyear75 <- Auto.df.notyear75 %>%
dplyr::rename(mpgclass = mpg) %>%
mutate(mpgclass = case_when(mpgclass < 20 ~'low',
between(mpgclass, 20, 27) ~ 'medium',
TRUE ~ 'high'))
mpg.notyear75 <- replace(mpg.notyear75, mpg.notyear75 < 20, 'low')
mpg.notyear75 <- replace(mpg.notyear75, mpg.notyear75 >= 20 & mpg.notyear75 < 27, 'medium')
mpg.notyear75 <- replace(mpg.notyear75, mpg.notyear75 >= 27, 'high')
Auto.df.year75 <- as_tibble(Auto.df.year75)
Auto.df.year75 <- Auto.df.year75 %>%
dplyr::rename(mpgclass = mpg) %>%
mutate(mpgclass = case_when(mpgclass < 20 ~'low',
between(mpgclass, 20, 27) ~ 'medium',
TRUE ~ 'high'))
mpg.year75 <- replace(mpg.year75, mpg.year75 < 20, 'low')
mpg.year75 <- replace(mpg.year75, mpg.year75 >= 20 & mpg.year75 < 27, 'medium')
mpg.year75 <- replace(mpg.year75, mpg.year75 >= 27, 'high')
I then want to apply lda and show the results in a confusion matrix, similar to this:
lda.fit <- lda(mpgclass ~ acceleration + displacement + horsepower + weight, data = Auto.df)
lda.pred <- predict(lda.fit, data = Auto.df)
with(Auto.df, table(lda.pred$class, mpgclass))
How is this done properly (and cleanly, rather than the mish-mash I have above)?
My data is as follows:
structure(list(mpg = c(18, 15, 18, 16, 17, 15, 14, 14, 14, 15,
15, 14, 15, 14, 24, 22, 18, 21, 27, 26, 25, 24, 25, 26, 21, 10,
10, 11, 9, 27, 28, 25, 19, 16, 17, 19, 18, 14, 14, 14, 14, 12,
13, 13, 18, 22, 19, 18, 23, 28, 30, 30, 31, 35, 27, 26, 24, 25,
23, 20, 21, 13, 14, 15, 14, 17, 11, 13, 12, 13, 19, 15, 13, 13,
14, 18, 22, 21, 26, 22, 28, 23, 28, 27, 13, 14, 13, 14, 15, 12,
13, 13, 14, 13, 12, 13, 18, 16, 18, 18, 23, 26, 11, 12, 13, 12,
18, 20, 21, 22, 18, 19, 21, 26, 15, 16, 29, 24, 20, 19, 15, 24,
20, 11, 20, 19, 15, 31, 26, 32, 25, 16, 16, 18, 16, 13, 14, 14,
14, 29, 26, 26, 31, 32, 28, 24, 26, 24, 26, 31, 19, 18, 15, 15,
16, 15, 16, 14, 17, 16, 15, 18, 21, 20, 13, 29, 23, 20, 23, 24,
25, 24, 18, 29, 19, 23, 23, 22, 25, 33, 28, 25, 25, 26, 27, 17.5,
16, 15.5, 14.5, 22, 22, 24, 22.5, 29, 24.5, 29, 33, 20, 18, 18.5,
17.5, 29.5, 32, 28, 26.5, 20, 13, 19, 19, 16.5, 16.5, 13, 13,
13, 31.5, 30, 36, 25.5, 33.5, 17.5, 17, 15.5, 15, 17.5, 20.5,
19, 18.5, 16, 15.5, 15.5, 16, 29, 24.5, 26, 25.5, 30.5, 33.5,
30, 30.5, 22, 21.5, 21.5, 43.1, 36.1, 32.8, 39.4, 36.1, 19.9,
19.4, 20.2, 19.2, 20.5, 20.2, 25.1, 20.5, 19.4, 20.6, 20.8, 18.6,
18.1, 19.2, 17.7, 18.1, 17.5, 30, 27.5, 27.2, 30.9, 21.1, 23.2,
23.8, 23.9, 20.3, 17, 21.6, 16.2, 31.5, 29.5, 21.5, 19.8, 22.3,
20.2, 20.6, 17, 17.6, 16.5, 18.2, 16.9, 15.5, 19.2, 18.5, 31.9,
34.1, 35.7, 27.4, 25.4, 23, 27.2, 23.9, 34.2, 34.5, 31.8, 37.3,
28.4, 28.8, 26.8, 33.5, 41.5, 38.1, 32.1, 37.2, 28, 26.4, 24.3,
19.1, 34.3, 29.8, 31.3, 37, 32.2, 46.6, 27.9, 40.8, 44.3, 43.4,
36.4, 30, 44.6, 33.8, 29.8, 32.7, 23.7, 35, 32.4, 27.2, 26.6,
25.8, 23.5, 30, 39.1, 39, 35.1, 32.3, 37, 37.7, 34.1, 34.7, 34.4,
29.9, 33, 33.7, 32.4, 32.9, 31.6, 28.1, 30.7, 25.4, 24.2, 22.4,
26.6, 20.2, 17.6, 28, 27, 34, 31, 29, 27, 24, 36, 37, 31, 38,
36, 36, 36, 34, 38, 32, 38, 25, 38, 26, 22, 32, 36, 27, 27, 44,
32, 28, 31), cylinders = c(8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
8, 8, 4, 6, 6, 6, 4, 4, 4, 4, 4, 4, 6, 8, 8, 8, 8, 4, 4, 4, 6,
6, 6, 6, 6, 8, 8, 8, 8, 8, 8, 8, 6, 4, 6, 6, 4, 4, 4, 4, 4, 4,
4, 4, 4, 4, 4, 4, 4, 8, 8, 8, 8, 8, 8, 8, 8, 8, 3, 8, 8, 8, 8,
4, 4, 4, 4, 4, 4, 4, 4, 4, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
6, 6, 6, 6, 6, 4, 8, 8, 8, 8, 6, 4, 4, 4, 3, 4, 6, 4, 8, 8, 4,
4, 4, 4, 8, 4, 6, 8, 6, 6, 6, 4, 4, 4, 4, 6, 6, 6, 8, 8, 8, 8,
8, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 6, 6, 6, 6, 8, 8, 8, 8, 6,
6, 6, 6, 6, 8, 8, 4, 4, 6, 4, 4, 4, 4, 6, 4, 6, 4, 4, 4, 4, 4,
4, 4, 4, 4, 4, 8, 8, 8, 8, 6, 6, 6, 6, 4, 4, 4, 4, 6, 6, 6, 6,
4, 4, 4, 4, 4, 8, 4, 6, 6, 8, 8, 8, 8, 4, 4, 4, 4, 4, 8, 8, 8,
8, 6, 6, 6, 6, 8, 8, 8, 8, 4, 4, 4, 4, 4, 4, 4, 4, 6, 4, 3, 4,
4, 4, 4, 4, 8, 8, 8, 6, 6, 6, 4, 6, 6, 6, 6, 6, 6, 8, 6, 8, 8,
4, 4, 4, 4, 4, 4, 4, 4, 5, 6, 4, 6, 4, 4, 6, 6, 4, 6, 6, 8, 8,
8, 8, 8, 8, 8, 8, 4, 4, 4, 4, 5, 8, 4, 8, 4, 4, 4, 4, 4, 6, 6,
4, 4, 4, 4, 4, 4, 4, 4, 6, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 5, 4,
4, 4, 4, 6, 3, 4, 4, 4, 4, 4, 6, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
4, 4, 4, 4, 4, 4, 4, 6, 6, 6, 6, 8, 6, 6, 4, 4, 4, 4, 4, 4, 4,
4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 6, 6, 4, 6, 4, 4, 4, 4, 4, 4,
4, 4), displacement = c(307, 350, 318, 304, 302, 429, 454, 440,
455, 390, 383, 340, 400, 455, 113, 198, 199, 200, 97, 97, 110,
107, 104, 121, 199, 360, 307, 318, 304, 97, 140, 113, 232, 225,
250, 250, 232, 350, 400, 351, 318, 383, 400, 400, 258, 140, 250,
250, 122, 116, 79, 88, 71, 72, 97, 91, 113, 97.5, 97, 140, 122,
350, 400, 318, 351, 304, 429, 350, 350, 400, 70, 304, 307, 302,
318, 121, 121, 120, 96, 122, 97, 120, 98, 97, 350, 304, 350,
302, 318, 429, 400, 351, 318, 440, 455, 360, 225, 250, 232, 250,
198, 97, 400, 400, 360, 350, 232, 97, 140, 108, 70, 122, 155,
98, 350, 400, 68, 116, 114, 121, 318, 121, 156, 350, 198, 232,
250, 79, 122, 71, 140, 250, 258, 225, 302, 350, 318, 302, 304,
98, 79, 97, 76, 83, 90, 90, 116, 120, 108, 79, 225, 250, 250,
250, 400, 350, 318, 351, 231, 250, 258, 225, 231, 262, 302, 97,
140, 232, 140, 134, 90, 119, 171, 90, 232, 115, 120, 121, 121,
91, 107, 116, 140, 98, 101, 305, 318, 304, 351, 225, 250, 200,
232, 85, 98, 90, 91, 225, 250, 250, 258, 97, 85, 97, 140, 130,
318, 120, 156, 168, 350, 350, 302, 318, 98, 111, 79, 122, 85,
305, 260, 318, 302, 250, 231, 225, 250, 400, 350, 400, 351, 97,
151, 97, 140, 98, 98, 97, 97, 146, 121, 80, 90, 98, 78, 85, 91,
260, 318, 302, 231, 200, 200, 140, 225, 232, 231, 200, 225, 258,
305, 231, 302, 318, 98, 134, 119, 105, 134, 156, 151, 119, 131,
163, 121, 163, 89, 98, 231, 200, 140, 232, 225, 305, 302, 351,
318, 350, 351, 267, 360, 89, 86, 98, 121, 183, 350, 141, 260,
105, 105, 85, 91, 151, 173, 173, 151, 98, 89, 98, 86, 151, 140,
151, 225, 97, 134, 120, 119, 108, 86, 156, 85, 90, 90, 121, 146,
91, 97, 89, 168, 70, 122, 107, 135, 151, 156, 173, 135, 79, 86,
81, 97, 85, 89, 91, 105, 98, 98, 105, 107, 108, 119, 120, 141,
145, 168, 146, 231, 350, 200, 225, 112, 112, 112, 112, 135, 151,
140, 105, 91, 91, 105, 98, 120, 107, 108, 91, 91, 91, 181, 262,
156, 232, 144, 135, 151, 140, 97, 135, 120, 119), horsepower = c(130,
165, 150, 150, 140, 198, 220, 215, 225, 190, 170, 160, 150, 225,
95, 95, 97, 85, 88, 46, 87, 90, 95, 113, 90, 215, 200, 210, 193,
88, 90, 95, 100, 105, 100, 88, 100, 165, 175, 153, 150, 180,
170, 175, 110, 72, 100, 88, 86, 90, 70, 76, 65, 69, 60, 70, 95,
80, 54, 90, 86, 165, 175, 150, 153, 150, 208, 155, 160, 190,
97, 150, 130, 140, 150, 112, 76, 87, 69, 86, 92, 97, 80, 88,
175, 150, 145, 137, 150, 198, 150, 158, 150, 215, 225, 175, 105,
100, 100, 88, 95, 46, 150, 167, 170, 180, 100, 88, 72, 94, 90,
85, 107, 90, 145, 230, 49, 75, 91, 112, 150, 110, 122, 180, 95,
100, 100, 67, 80, 65, 75, 100, 110, 105, 140, 150, 150, 140,
150, 83, 67, 78, 52, 61, 75, 75, 75, 97, 93, 67, 95, 105, 72,
72, 170, 145, 150, 148, 110, 105, 110, 95, 110, 110, 129, 75,
83, 100, 78, 96, 71, 97, 97, 70, 90, 95, 88, 98, 115, 53, 86,
81, 92, 79, 83, 140, 150, 120, 152, 100, 105, 81, 90, 52, 60,
70, 53, 100, 78, 110, 95, 71, 70, 75, 72, 102, 150, 88, 108,
120, 180, 145, 130, 150, 68, 80, 58, 96, 70, 145, 110, 145, 130,
110, 105, 100, 98, 180, 170, 190, 149, 78, 88, 75, 89, 63, 83,
67, 78, 97, 110, 110, 48, 66, 52, 70, 60, 110, 140, 139, 105,
95, 85, 88, 100, 90, 105, 85, 110, 120, 145, 165, 139, 140, 68,
95, 97, 75, 95, 105, 85, 97, 103, 125, 115, 133, 71, 68, 115,
85, 88, 90, 110, 130, 129, 138, 135, 155, 142, 125, 150, 71,
65, 80, 80, 77, 125, 71, 90, 70, 70, 65, 69, 90, 115, 115, 90,
76, 60, 70, 65, 90, 88, 90, 90, 78, 90, 75, 92, 75, 65, 105,
65, 48, 48, 67, 67, 67, 67, 62, 132, 100, 88, 72, 84, 84, 92,
110, 84, 58, 64, 60, 67, 65, 62, 68, 63, 65, 65, 74, 75, 75,
100, 74, 80, 76, 116, 120, 110, 105, 88, 85, 88, 88, 88, 85,
84, 90, 92, 74, 68, 68, 63, 70, 88, 75, 70, 67, 67, 67, 110,
85, 92, 112, 96, 84, 90, 86, 52, 84, 79, 82), weight = c(3504,
3693, 3436, 3433, 3449, 4341, 4354, 4312, 4425, 3850, 3563, 3609,
3761, 3086, 2372, 2833, 2774, 2587, 2130, 1835, 2672, 2430, 2375,
2234, 2648, 4615, 4376, 4382, 4732, 2130, 2264, 2228, 2634, 3439,
3329, 3302, 3288, 4209, 4464, 4154, 4096, 4955, 4746, 5140, 2962,
2408, 3282, 3139, 2220, 2123, 2074, 2065, 1773, 1613, 1834, 1955,
2278, 2126, 2254, 2408, 2226, 4274, 4385, 4135, 4129, 3672, 4633,
4502, 4456, 4422, 2330, 3892, 4098, 4294, 4077, 2933, 2511, 2979,
2189, 2395, 2288, 2506, 2164, 2100, 4100, 3672, 3988, 4042, 3777,
4952, 4464, 4363, 4237, 4735, 4951, 3821, 3121, 3278, 2945, 3021,
2904, 1950, 4997, 4906, 4654, 4499, 2789, 2279, 2401, 2379, 2124,
2310, 2472, 2265, 4082, 4278, 1867, 2158, 2582, 2868, 3399, 2660,
2807, 3664, 3102, 2901, 3336, 1950, 2451, 1836, 2542, 3781, 3632,
3613, 4141, 4699, 4457, 4638, 4257, 2219, 1963, 2300, 1649, 2003,
2125, 2108, 2246, 2489, 2391, 2000, 3264, 3459, 3432, 3158, 4668,
4440, 4498, 4657, 3907, 3897, 3730, 3785, 3039, 3221, 3169, 2171,
2639, 2914, 2592, 2702, 2223, 2545, 2984, 1937, 3211, 2694, 2957,
2945, 2671, 1795, 2464, 2220, 2572, 2255, 2202, 4215, 4190, 3962,
4215, 3233, 3353, 3012, 3085, 2035, 2164, 1937, 1795, 3651, 3574,
3645, 3193, 1825, 1990, 2155, 2565, 3150, 3940, 3270, 2930, 3820,
4380, 4055, 3870, 3755, 2045, 2155, 1825, 2300, 1945, 3880, 4060,
4140, 4295, 3520, 3425, 3630, 3525, 4220, 4165, 4325, 4335, 1940,
2740, 2265, 2755, 2051, 2075, 1985, 2190, 2815, 2600, 2720, 1985,
1800, 1985, 2070, 1800, 3365, 3735, 3570, 3535, 3155, 2965, 2720,
3430, 3210, 3380, 3070, 3620, 3410, 3425, 3445, 3205, 4080, 2155,
2560, 2300, 2230, 2515, 2745, 2855, 2405, 2830, 3140, 2795, 3410,
1990, 2135, 3245, 2990, 2890, 3265, 3360, 3840, 3725, 3955, 3830,
4360, 4054, 3605, 3940, 1925, 1975, 1915, 2670, 3530, 3900, 3190,
3420, 2200, 2150, 2020, 2130, 2670, 2595, 2700, 2556, 2144, 1968,
2120, 2019, 2678, 2870, 3003, 3381, 2188, 2711, 2542, 2434, 2265,
2110, 2800, 2110, 2085, 2335, 2950, 3250, 1850, 2145, 1845, 2910,
2420, 2500, 2290, 2490, 2635, 2620, 2725, 2385, 1755, 1875, 1760,
2065, 1975, 2050, 1985, 2215, 2045, 2380, 2190, 2210, 2350, 2615,
2635, 3230, 3160, 2900, 2930, 3415, 3725, 3060, 3465, 2605, 2640,
2395, 2575, 2525, 2735, 2865, 1980, 2025, 1970, 2125, 2125, 2160,
2205, 2245, 1965, 1965, 1995, 2945, 3015, 2585, 2835, 2665, 2370,
2950, 2790, 2130, 2295, 2625, 2720), acceleration = c(12, 11.5,
11, 12, 10.5, 10, 9, 8.5, 10, 8.5, 10, 8, 9.5, 10, 15, 15.5,
15.5, 16, 14.5, 20.5, 17.5, 14.5, 17.5, 12.5, 15, 14, 15, 13.5,
18.5, 14.5, 15.5, 14, 13, 15.5, 15.5, 15.5, 15.5, 12, 11.5, 13.5,
13, 11.5, 12, 12, 13.5, 19, 15, 14.5, 14, 14, 19.5, 14.5, 19,
18, 19, 20.5, 15.5, 17, 23.5, 19.5, 16.5, 12, 12, 13.5, 13, 11.5,
11, 13.5, 13.5, 12.5, 13.5, 12.5, 14, 16, 14, 14.5, 18, 19.5,
18, 16, 17, 14.5, 15, 16.5, 13, 11.5, 13, 14.5, 12.5, 11.5, 12,
13, 14.5, 11, 11, 11, 16.5, 18, 16, 16.5, 16, 21, 14, 12.5, 13,
12.5, 15, 19, 19.5, 16.5, 13.5, 18.5, 14, 15.5, 13, 9.5, 19.5,
15.5, 14, 15.5, 11, 14, 13.5, 11, 16.5, 16, 17, 19, 16.5, 21,
17, 17, 18, 16.5, 14, 14.5, 13.5, 16, 15.5, 16.5, 15.5, 14.5,
16.5, 19, 14.5, 15.5, 14, 15, 15.5, 16, 16, 16, 21, 19.5, 11.5,
14, 14.5, 13.5, 21, 18.5, 19, 19, 15, 13.5, 12, 16, 17, 16, 18.5,
13.5, 16.5, 17, 14.5, 14, 17, 15, 17, 14.5, 13.5, 17.5, 15.5,
16.9, 14.9, 17.7, 15.3, 13, 13, 13.9, 12.8, 15.4, 14.5, 17.6,
17.6, 22.2, 22.1, 14.2, 17.4, 17.7, 21, 16.2, 17.8, 12.2, 17,
16.4, 13.6, 15.7, 13.2, 21.9, 15.5, 16.7, 12.1, 12, 15, 14, 18.5,
14.8, 18.6, 15.5, 16.8, 12.5, 19, 13.7, 14.9, 16.4, 16.9, 17.7,
19, 11.1, 11.4, 12.2, 14.5, 14.5, 16, 18.2, 15.8, 17, 15.9, 16.4,
14.1, 14.5, 12.8, 13.5, 21.5, 14.4, 19.4, 18.6, 16.4, 15.5, 13.2,
12.8, 19.2, 18.2, 15.8, 15.4, 17.2, 17.2, 15.8, 16.7, 18.7, 15.1,
13.2, 13.4, 11.2, 13.7, 16.5, 14.2, 14.7, 14.5, 14.8, 16.7, 17.6,
14.9, 15.9, 13.6, 15.7, 15.8, 14.9, 16.6, 15.4, 18.2, 17.3, 18.2,
16.6, 15.4, 13.4, 13.2, 15.2, 14.9, 14.3, 15, 13, 14, 15.2, 14.4,
15, 20.1, 17.4, 24.8, 22.2, 13.2, 14.9, 19.2, 14.7, 16, 11.3,
12.9, 13.2, 14.7, 18.8, 15.5, 16.4, 16.5, 18.1, 20.1, 18.7, 15.8,
15.5, 17.5, 15, 15.2, 17.9, 14.4, 19.2, 21.7, 23.7, 19.9, 21.8,
13.8, 18, 15.3, 11.4, 12.5, 15.1, 17, 15.7, 16.4, 14.4, 12.6,
12.9, 16.9, 16.4, 16.1, 17.8, 19.4, 17.3, 16, 14.9, 16.2, 20.7,
14.2, 14.4, 16.8, 14.8, 18.3, 20.4, 19.6, 12.6, 13.8, 15.8, 19,
17.1, 16.6, 19.6, 18.6, 18, 16.2, 16, 18, 16.4, 15.3, 18.2, 17.6,
14.7, 17.3, 14.5, 14.5, 16.9, 15, 15.7, 16.2, 16.4, 17, 14.5,
14.7, 13.9, 13, 17.3, 15.6, 24.6, 11.6, 18.6, 19.4), year = c(70,
70, 70, 70, 70, 70, 70, 70, 70, 70, 70, 70, 70, 70, 70, 70, 70,
70, 70, 70, 70, 70, 70, 70, 70, 70, 70, 70, 70, 71, 71, 71, 71,
71, 71, 71, 71, 71, 71, 71, 71, 71, 71, 71, 71, 71, 71, 71, 71,
71, 71, 71, 71, 71, 71, 71, 72, 72, 72, 72, 72, 72, 72, 72, 72,
72, 72, 72, 72, 72, 72, 72, 72, 72, 72, 72, 72, 72, 72, 72, 72,
72, 72, 72, 73, 73, 73, 73, 73, 73, 73, 73, 73, 73, 73, 73, 73,
73, 73, 73, 73, 73, 73, 73, 73, 73, 73, 73, 73, 73, 73, 73, 73,
73, 73, 73, 73, 73, 73, 73, 73, 73, 73, 73, 74, 74, 74, 74, 74,
74, 74, 74, 74, 74, 74, 74, 74, 74, 74, 74, 74, 74, 74, 74, 74,
74, 74, 74, 74, 74, 75, 75, 75, 75, 75, 75, 75, 75, 75, 75, 75,
75, 75, 75, 75, 75, 75, 75, 75, 75, 75, 75, 75, 75, 75, 75, 75,
75, 75, 75, 76, 76, 76, 76, 76, 76, 76, 76, 76, 76, 76, 76, 76,
76, 76, 76, 76, 76, 76, 76, 76, 76, 76, 76, 76, 76, 76, 76, 76,
76, 76, 76, 76, 76, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77,
77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77,
77, 78, 78, 78, 78, 78, 78, 78, 78, 78, 78, 78, 78, 78, 78, 78,
78, 78, 78, 78, 78, 78, 78, 78, 78, 78, 78, 78, 78, 78, 78, 78,
78, 78, 78, 78, 78, 79, 79, 79, 79, 79, 79, 79, 79, 79, 79, 79,
79, 79, 79, 79, 79, 79, 79, 79, 79, 79, 79, 79, 79, 79, 79, 79,
79, 79, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80,
80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 81, 81, 81,
81, 81, 81, 81, 81, 81, 81, 81, 81, 81, 81, 81, 81, 81, 81, 81,
81, 81, 81, 81, 81, 81, 81, 81, 81, 82, 82, 82, 82, 82, 82, 82,
82, 82, 82, 82, 82, 82, 82, 82, 82, 82, 82, 82, 82, 82, 82, 82,
82, 82, 82, 82, 82, 82, 82), origin = c(1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 3, 1, 1, 1, 3, 2, 2, 2, 2, 2, 1, 1, 1, 1,
1, 3, 1, 3, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
2, 2, 2, 3, 3, 2, 1, 3, 1, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
3, 1, 1, 1, 1, 2, 2, 2, 2, 1, 3, 3, 1, 3, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 3, 1, 3, 3, 1,
1, 2, 1, 1, 2, 2, 2, 2, 1, 2, 3, 1, 1, 1, 1, 3, 1, 3, 1, 1, 1,
1, 1, 1, 1, 1, 1, 2, 2, 2, 3, 3, 1, 2, 2, 3, 3, 2, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 3, 1, 1, 1, 3, 2, 3, 1, 2, 1,
2, 2, 2, 2, 3, 2, 2, 1, 1, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2,
3, 1, 1, 1, 1, 2, 3, 3, 1, 2, 1, 2, 3, 2, 1, 1, 1, 1, 3, 1, 2,
1, 3, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 1, 3, 1, 1, 1, 3,
2, 3, 2, 3, 2, 1, 3, 3, 3, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 3, 3, 1, 3, 1, 1, 3, 2, 2, 2, 2, 2, 3, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 3, 1, 1, 2, 1, 2, 1, 1, 1,
3, 2, 1, 1, 1, 1, 2, 3, 1, 3, 1, 1, 1, 1, 2, 3, 3, 3, 3, 3, 1,
3, 2, 2, 2, 2, 3, 3, 2, 3, 3, 2, 3, 1, 1, 1, 1, 1, 3, 1, 3, 3,
3, 3, 3, 1, 1, 1, 2, 3, 3, 3, 3, 2, 2, 3, 3, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 2, 3, 3, 1, 1, 3, 3, 3, 3, 3, 3, 1, 1, 1, 1, 3,
1, 1, 1, 2, 1, 1, 1), name = structure(c(49L, 36L, 231L, 14L,
161L, 141L, 54L, 223L, 241L, 2L, 101L, 215L, 57L, 30L, 274L,
220L, 8L, 150L, 95L, 282L, 211L, 16L, 258L, 22L, 7L, 133L, 68L,
112L, 163L, 95L, 65L, 271L, 7L, 232L, 49L, 162L, 10L, 54L, 242L,
141L, 223L, 116L, 129L, 250L, 9L, 64L, 243L, 151L, 187L, 208L,
210L, 123L, 267L, 76L, 286L, 218L, 272L, 105L, 293L, 63L, 158L,
54L, 241L, 223L, 141L, 3L, 192L, 31L, 203L, 75L, 183L, 11L, 48L,
143L, 233L, 295L, 283L, 212L, 253L, 157L, 85L, 279L, 104L, 268L,
25L, 10L, 55L, 142L, 109L, 193L, 44L, 148L, 222L, 74L, 29L, 1L,
235L, 62L, 8L, 150L, 220L, 292L, 54L, 128L, 219L, 207L, 7L, 263L,
63L, 87L, 174L, 156L, 188L, 121L, 59L, 244L, 124L, 209L, 17L,
294L, 113L, 260L, 276L, 204L, 220L, 8L, 61L, 92L, 156L, 267L,
63L, 50L, 10L, 234L, 142L, 27L, 110L, 143L, 11L, 21L, 284L, 209L,
271L, 88L, 103L, 124L, 122L, 167L, 261L, 127L, 236L, 61L, 194L,
150L, 241L, 42L, 224L, 148L, 24L, 41L, 10L, 221L, 34L, 60L, 154L,
266L, 156L, 7L, 240L, 271L, 284L, 88L, 156L, 287L, 12L, 17L,
211L, 296L, 260L, 171L, 125L, 208L, 40L, 103L, 254L, 50L, 108L,
10L, 142L, 235L, 61L, 150L, 8L, 51L, 66L, 302L, 167L, 100L, 145L,
252L, 13L, 287L, 91L, 266L, 156L, 297L, 239L, 211L, 276L, 185L,
39L, 67L, 132L, 111L, 165L, 32L, 256L, 216L, 94L, 44L, 202L,
117L, 189L, 53L, 35L, 238L, 144L, 245L, 58L, 70L, 160L, 288L,
251L, 269L, 155L, 51L, 107L, 262L, 284L, 89L, 23L, 181L, 289L,
139L, 180L, 93L, 171L, 201L, 114L, 195L, 249L, 55L, 135L, 136L,
237L, 4L, 28L, 196L, 98L, 5L, 58L, 33L, 140L, 115L, 51L, 271L,
84L, 118L, 265L, 230L, 206L, 77L, 19L, 298L, 259L, 214L, 291L,
166L, 247L, 197L, 137L, 6L, 99L, 44L, 149L, 190L, 120L, 30L,
129L, 56L, 73L, 304L, 173L, 106L, 15L, 186L, 38L, 211L, 201L,
225L, 228L, 79L, 126L, 37L, 52L, 205L, 248L, 302L, 270L, 51L,
82L, 52L, 134L, 4L, 98L, 18L, 273L, 175L, 86L, 266L, 176L, 103L,
79L, 303L, 300L, 20L, 184L, 170L, 262L, 281L, 81L, 182L, 280L,
164L, 229L, 35L, 97L, 52L, 229L, 277L, 217L, 169L, 261L, 80L,
278L, 177L, 226L, 131L, 130L, 285L, 172L, 266L, 78L, 175L, 213L,
299L, 275L, 90L, 24L, 200L, 146L, 72L, 45L, 47L, 46L, 246L, 96L,
248L, 138L, 290L, 179L, 178L, 227L, 191L, 198L, 164L, 266L, 167L,
168L, 83L, 26L, 199L, 71L, 147L, 264L, 102L, 43L, 153L, 301L,
119L, 159L, 69L), .Label = c("amc ambassador brougham", "amc ambassador dpl",
"amc ambassador sst", "amc concord", "amc concord d/l", "amc concord dl 6",
"amc gremlin", "amc hornet", "amc hornet sportabout (sw)", "amc matador",
"amc matador (sw)", "amc pacer", "amc pacer d/l", "amc rebel sst",
"amc spirit dl", "audi 100 ls", "audi 100ls", "audi 4000", "audi 5000",
"audi 5000s (diesel)", "audi fox", "bmw 2002", "bmw 320i", "buick century",
"buick century 350", "buick century limited", "buick century luxus (sw)",
"buick century special", "buick electra 225 custom", "buick estate wagon (sw)",
"buick lesabre custom", "buick opel isuzu deluxe", "buick regal sport coupe (turbo)",
"buick skyhawk", "buick skylark", "buick skylark 320", "buick skylark limited",
"cadillac eldorado", "cadillac seville", "capri ii", "chevroelt chevelle malibu",
"chevrolet bel air", "chevrolet camaro", "chevrolet caprice classic",
"chevrolet cavalier", "chevrolet cavalier 2-door", "chevrolet cavalier wagon",
"chevrolet chevelle concours (sw)", "chevrolet chevelle malibu",
"chevrolet chevelle malibu classic", "chevrolet chevette", "chevrolet citation",
"chevrolet concours", "chevrolet impala", "chevrolet malibu",
"chevrolet malibu classic (sw)", "chevrolet monte carlo", "chevrolet monte carlo landau",
"chevrolet monte carlo s", "chevrolet monza 2+2", "chevrolet nova",
"chevrolet nova custom", "chevrolet vega", "chevrolet vega (sw)",
"chevrolet vega 2300", "chevrolet woody", "chevy c10", "chevy c20",
"chevy s-10", "chrysler cordoba", "chrysler lebaron medallion",
"chrysler lebaron salon", "chrysler lebaron town # country (sw)",
"chrysler new yorker brougham", "chrysler newport royal", "datsun 1200",
"datsun 200-sx", "datsun 200sx", "datsun 210", "datsun 210 mpg",
"datsun 280-zx", "datsun 310", "datsun 310 gx", "datsun 510",
"datsun 510 (sw)", "datsun 510 hatchback", "datsun 610", "datsun 710",
"datsun 810", "datsun 810 maxima", "datsun b-210", "datsun b210",
"datsun b210 gx", "datsun f-10 hatchback", "datsun pl510", "dodge aries se",
"dodge aries wagon (sw)", "dodge aspen", "dodge aspen 6", "dodge aspen se",
"dodge challenger se", "dodge charger 2.2", "dodge colt", "dodge colt (sw)",
"dodge colt hardtop", "dodge colt hatchback custom", "dodge colt m/m",
"dodge coronet brougham", "dodge coronet custom", "dodge coronet custom (sw)",
"dodge d100", "dodge d200", "dodge dart custom", "dodge diplomat",
"dodge magnum xe", "dodge monaco (sw)", "dodge monaco brougham",
"dodge omni", "dodge rampage", "dodge st. regis", "fiat 124 sport coupe",
"fiat 124 tc", "fiat 124b", "fiat 128", "fiat 131", "fiat strada custom",
"fiat x1.9", "ford country", "ford country squire (sw)", "ford escort 2h",
"ford escort 4w", "ford f108", "ford f250", "ford fairmont",
"ford fairmont (auto)", "ford fairmont (man)", "ford fairmont 4",
"ford fairmont futura", "ford fiesta", "ford futura", "ford galaxie 500",
"ford gran torino", "ford gran torino (sw)", "ford granada",
"ford granada ghia", "ford granada gl", "ford granada l", "ford ltd",
"ford ltd landau", "ford maverick", "ford mustang", "ford mustang cobra",
"ford mustang gl", "ford mustang ii", "ford mustang ii 2+2",
"ford pinto", "ford pinto (sw)", "ford pinto runabout", "ford ranger",
"ford thunderbird", "ford torino", "ford torino 500", "hi 1200d",
"honda accord", "honda accord cvcc", "honda accord lx", "honda civic",
"honda civic (auto)", "honda civic 1300", "honda civic 1500 gl",
"honda civic cvcc", "honda prelude", "maxda glc deluxe", "maxda rx3",
"mazda 626", "mazda glc", "mazda glc 4", "mazda glc custom",
"mazda glc custom l", "mazda glc deluxe", "mazda rx-4", "mazda rx-7 gs",
"mazda rx2 coupe", "mercedes-benz 240d", "mercedes-benz 280s",
"mercedes benz 300d", "mercury capri 2000", "mercury capri v6",
"mercury cougar brougham", "mercury grand marquis", "mercury lynx l",
"mercury marquis", "mercury marquis brougham", "mercury monarch",
"mercury monarch ghia", "mercury zephyr", "mercury zephyr 6",
"nissan stanza xe", "oldsmobile cutlass ciera (diesel)", "oldsmobile cutlass ls",
"oldsmobile cutlass salon brougham", "oldsmobile cutlass supreme",
"oldsmobile delta 88 royale", "oldsmobile omega", "oldsmobile omega brougham",
"oldsmobile starfire sx", "oldsmobile vista cruiser", "opel 1900",
"opel manta", "peugeot 304", "peugeot 504", "peugeot 504 (sw)",
"peugeot 505s turbo diesel", "peugeot 604sl", "plymouth 'cuda 340",
"plymouth arrow gs", "plymouth champ", "plymouth cricket", "plymouth custom suburb",
"plymouth duster", "plymouth fury", "plymouth fury gran sedan",
"plymouth fury iii", "plymouth grand fury", "plymouth horizon",
"plymouth horizon 4", "plymouth horizon miser", "plymouth horizon tc3",
"plymouth reliant", "plymouth sapporo", "plymouth satellite",
"plymouth satellite custom", "plymouth satellite custom (sw)",
"plymouth satellite sebring", "plymouth valiant", "plymouth valiant custom",
"plymouth volare", "plymouth volare custom", "plymouth volare premier v8",
"pontiac astro", "pontiac catalina", "pontiac catalina brougham",
"pontiac firebird", "pontiac grand prix", "pontiac grand prix lj",
"pontiac j2000 se hatchback", "pontiac lemans v6", "pontiac phoenix",
"pontiac phoenix lj", "pontiac safari (sw)", "pontiac sunbird coupe",
"pontiac ventura sj", "renault 12 (sw)", "renault 12tl", "renault 18i",
"renault 5 gtl", "renault lecar deluxe", "saab 99e", "saab 99gle",
"saab 99le", "subaru", "subaru dl", "toyota carina", "toyota celica gt",
"toyota celica gt liftback", "toyota corolla", "toyota corolla 1200",
"toyota corolla 1600 (sw)", "toyota corolla liftback", "toyota corolla tercel",
"toyota corona", "toyota corona hardtop", "toyota corona liftback",
"toyota corona mark ii", "toyota cressida", "toyota mark ii",
"toyota starlet", "toyota tercel", "toyouta corona mark ii (sw)",
"triumph tr7 coupe", "vokswagen rabbit", "volkswagen 1131 deluxe sedan",
"volkswagen 411 (sw)", "volkswagen dasher", "volkswagen jetta",
"volkswagen model 111", "volkswagen rabbit", "volkswagen rabbit custom",
"volkswagen rabbit custom diesel", "volkswagen rabbit l", "volkswagen scirocco",
"volkswagen super beetle", "volkswagen type 3", "volvo 144ea",
"volvo 145e (sw)", "volvo 244dl", "volvo 245", "volvo 264gl",
"volvo diesel", "vw dasher (diesel)", "vw pickup", "vw rabbit",
"vw rabbit c (diesel)", "vw rabbit custom"), class = "factor")), row.names = c("1",
"2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13",
"14", "15", "16", "17", "18", "19", "20", "21", "22", "23", "24",
"25", "26", "27", "28", "29", "30", "31", "32", "34", "35", "36",
"37", "38", "39", "40", "41", "42", "43", "44", "45", "46", "47",
"48", "49", "50", "51", "52", "53", "54", "55", "56", "57", "58",
"59", "60", "61", "62", "63", "64", "65", "66", "67", "68", "69",
"70", "71", "72", "73", "74", "75", "76", "77", "78", "79", "80",
"81", "82", "83", "84", "85", "86", "87", "88", "89", "90", "91",
"92", "93", "94", "95", "96", "97", "98", "99", "100", "101",
"102", "103", "104", "105", "106", "107", "108", "109", "110",
"111", "112", "113", "114", "115", "116", "117", "118", "119",
"120", "121", "122", "123", "124", "125", "126", "128", "129",
"130", "131", "132", "133", "134", "135", "136", "137", "138",
"139", "140", "141", "142", "143", "144", "145", "146", "147",
"148", "149", "150", "151", "152", "153", "154", "155", "156",
"157", "158", "159", "160", "161", "162", "163", "164", "165",
"166", "167", "168", "169", "170", "171", "172", "173", "174",
"175", "176", "177", "178", "179", "180", "181", "182", "183",
"184", "185", "186", "187", "188", "189", "190", "191", "192",
"193", "194", "195", "196", "197", "198", "199", "200", "201",
"202", "203", "204", "205", "206", "207", "208", "209", "210",
"211", "212", "213", "214", "215", "216", "217", "218", "219",
"220", "221", "222", "223", "224", "225", "226", "227", "228",
"229", "230", "231", "232", "233", "234", "235", "236", "237",
"238", "239", "240", "241", "242", "243", "244", "245", "246",
"247", "248", "249", "250", "251", "252", "253", "254", "255",
"256", "257", "258", "259", "260", "261", "262", "263", "264",
"265", "266", "267", "268", "269", "270", "271", "272", "273",
"274", "275", "276", "277", "278", "279", "280", "281", "282",
"283", "284", "285", "286", "287", "288", "289", "290", "291",
"292", "293", "294", "295", "296", "297", "298", "299", "300",
"301", "302", "303", "304", "305", "306", "307", "308", "309",
"310", "311", "312", "313", "314", "315", "316", "317", "318",
"319", "320", "321", "322", "323", "324", "325", "326", "327",
"328", "329", "330", "332", "333", "334", "335", "336", "338",
"339", "340", "341", "342", "343", "344", "345", "346", "347",
"348", "349", "350", "351", "352", "353", "354", "356", "357",
"358", "359", "360", "361", "362", "363", "364", "365", "366",
"367", "368", "369", "370", "371", "372", "373", "374", "375",
"376", "377", "378", "379", "380", "381", "382", "383", "384",
"385", "386", "387", "388", "389", "390", "391", "392", "393",
"394", "395", "396", "397"), class = "data.frame")
The issue is in these 3 lines.
mpg.notyear75 <- replace(mpg.notyear75, mpg.notyear75 < 20, 'low')
mpg.notyear75 <- replace(mpg.notyear75, mpg.notyear75 >= 20 & mpg.notyear75 < 27, 'medium')
mpg.notyear75 <- replace(mpg.notyear75, mpg.notyear75 >= 27, 'high')
After running the 1st line some values have changed to 'low' whereas some of them are still numbers. After this there is no point running the 2nd (and 3rd) line since all the values are now characters.
You need to change all the values simultaneously and not one by one. Why not use case_when as you have already done.
library(dplyr)
mpg.notyear75 <- case_when(mpg.notyear75 < 20 ~'low',
between(mpg.notyear75, 20, 27) ~ 'medium',
TRUE ~ 'high')
I have a dataframe df
df<- structure(list(year = structure(c(72L, 2L, 13L, 24L, 35L, 46L,
57L, 68L, 70L, 71L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L,
14L, 15L, 16L, 17L, 18L, 19L, 20L, 21L, 22L, 23L, 25L, 26L, 27L,
28L, 29L, 30L, 31L, 32L, 33L, 34L, 36L, 37L, 38L, 39L, 40L, 41L,
42L, 43L, 44L, 45L, 47L, 48L, 49L, 50L, 51L, 52L, 53L, 54L, 55L,
56L, 58L, 59L, 60L, 61L, 62L, 63L, 64L, 65L, 66L, 67L, 69L), .Label = c("day",
"id.1.00000", "id.10.0000", "id.11.0000", "id.12.0000", "id.13.0000",
"id.14.0000", "id.15.0000", "id.16.0000", "id.17.0000", "id.18.0000",
"id.19.0000", "id.2.00000", "id.20.0000", "id.21.0000", "id.22.0000",
"id.23.0000", "id.24.0000", "id.25.0000", "id.26.0000", "id.27.0000",
"id.28.0000", "id.29.0000", "id.3.00000", "id.30.0000", "id.31.0000",
"id.32.0000", "id.33.0000", "id.34.0000", "id.35.0000", "id.36.0000",
"id.37.0000", "id.38.0000", "id.39.0000", "id.4.00000", "id.40.0000",
"id.41.0000", "id.42.0000", "id.43.0000", "id.44.0000", "id.45.0000",
"id.46.0000", "id.47.0000", "id.48.0000", "id.49.0000", "id.5.00000",
"id.50.0000", "id.51.0000", "id.52.0000", "id.53.0000", "id.54.0000",
"id.55.0000", "id.56.0000", "id.57.0000", "id.58.0000", "id.59.0000",
"id.6.00000", "id.60.0000", "id.61.0000", "id.62.0000", "id.63.0000",
"id.64.0000", "id.65.0000", "id.66.0000", "id.67.0000", "id.68.0000",
"id.69.0000", "id.7.00000", "id.70.0000", "id.8.00000", "id.9.00000",
"month"), class = "factor"), `2012` = c(1, 76.1, 38.1, 88.6,
132.5, 241.9, 343.2, 343.2, 291.7, 82.8, 17.6, 17.6, 17.5, 17.6,
17.6, 17.6, 17.5, 18.7, 20.4, 17.3, 16.8, 67.6, 66.2, 16.8, 16.8,
16.8, 121.8, 0.1, 99.9, 101.5, 52, 81.6, 68.5, 37.6, 46.2, 23.4,
31, 55.7, 24, 384.8, 53.3, 22.7, 18.6, 25.4, 42.9, 16.5, 75,
69.7, 43.2, 90.9, 18.5, 53.1, 21.6, 66.9, 40.8, 153.3, 47.2,
98.6, 75.8, 141.6, 9.8, 12.7, 12.7, 69.2, 18.3, 359, 153.4, 24.3,
24.3, 24.3, 7.9), `2012` = c(2, 104.6, 41.4, 24.8, 25, 299.3,
181.6, 181.6, 223, 49.1, 9.3, 9.3, 8.7, 9.3, 9.3, 9.3, 8.7, 9.6,
5.2, 3.9, 4.4, 29.2, 30.3, 4.4, 4.4, 4.4, 49.1, 7.2, 27.7, 12.7,
20.5, 15.2, 19.7, 4.8, 35.7, 31.6, 6, 7.4, 2.1, 452.5, 43.2,
92.8, 62, 17.3, 8.4, 14.2, 94.5, 60.1, 28.3, 18.8, 0, 49.1, 11.4,
47.7, 44.6, 73.3, 23, 62.9, 30.5, 95, 45.8, 42.4, 42.4, 31.1,
33.8, 340, 63.8, 12.9, 12.9, 12.9, 8.3), `2012` = c(3, 196.1,
75.8, 26.7, 27.5, 410.1, 287.8, 287.8, 254.9, 56.9, 24.6, 24.6,
20.8, 24.6, 24.6, 24.6, 20.8, 13.3, 21.3, 20.7, 22.9, 41.2, 42.9,
22.9, 22.9, 22.9, 63.4, 34.1, 15.6, 11.1, 8.5, 10.4, 8.1, 35.6,
27, 20.2, 18.1, 33.7, 45.3, 363.2, 32.3, 28.9, 14.6, 10.7, 45.6,
18.3, 159.2, 51.7, 33.6, 17.5, 52.8, 62.9, 20.5, 28.3, 21, 28.1,
24.3, 48.1, 41.6, 179.6, 2.7, 36.9, 36.9, 59, 41.4, 349, 59.2,
21.5, 21.5, 21.5, 31.7), `2012` = c(4, 27.5, 45.4, 87.6, 123.1,
290.6, 99.9, 99.9, 103.9, 33.7, 24.9, 24.9, 21.2, 24.9, 24.9,
24.9, 21.2, 15.6, 57.2, 48.3, 43.6, 51.8, 54.8, 43.6, 43.6, 43.6,
67.8, 58.6, 26.5, 25.5, 26.6, 19.3, 24.2, 24.8, 43.8, 39.2, 67.4,
190.8, 121.5, 462.7, 0, 79.8, 45.8, 23.6, 161.1, 120.7, 119.8,
44.4, 62, 66.4, 63.1, 68, 19.4, 50.8, 63.7, 122.9, 119.3, 103.1,
81.3, 97.1, 14.8, 36.6, 36.6, 35.3, 82.3, 313, 75.1, 18.6, 18.6,
18.6, 67.1), `2012` = c(5, 72.5, 102.2, 66.2, 70.8, 214.6, 73.4,
73.4, 69.7, 72.1, 35.5, 35.5, 31, 35.5, 35.5, 35.5, 31, 24.1,
67.8, 67, 60.1, 82.3, 86, 60.1, 60.1, 60.1, 48.5, 62.7, 53.4,
54.2, 42.9, 55.6, 19, 0, 47.5, 26.1, 91.5, 32.5, 51.6, 389.3,
0, 30.5, 10.9, 40.4, 89.5, 62.9, 123.9, 71.2, 151.6, 69.4, 101.4,
49.8, 27.1, 35, 19.6, 72.6, 31.7, 120, 143.3, 54.2, 8.8, 3.6,
3.6, 33.3, 143.8, 221, 59.5, 25.3, 25.3, 25.3, 314.6), `2012` = c(6,
101.8, 124.8, 117, 136.3, 194.3, 142.1, 142.1, 135.2, 87.9, 75.2,
75.2, 72.7, 75.2, 75.2, 75.2, 72.7, 61.5, 101.8, 108.6, 103.7,
125.8, 131.9, 103.7, 103.7, 103.7, 52.8, 200.5, 81.9, 103.6,
68.5, 83.2, 65.2, 1.2, 79.6, 64.7, 76.3, 73, 35.6, 238.4, 0,
13.6, 0.1, 26.7, 65, 17.5, 169.5, 36.3, 71.8, 86.4, 3.1, 117.9,
23.6, 61.6, 93.3, 143.1, 122.6, 167, 96.7, 77.6, 33.5, 0, 0,
94.8, 160, 220, 89.3, 23.3, 23.3, 23.3, 399.9), `2012` = c(7,
62.8, 68.9, 108.8, 180.2, 189.2, 51.8, 51.8, 46.2, 50.2, 68.7,
68.7, 64.1, 68.7, 68.7, 68.7, 64.1, 60, 107.8, 101.8, 90.7, 40.6,
41.7, 90.7, 90.7, 90.7, 53.8, 289.2, 72.9, 86.2, 119.4, 66.8,
59.4, 0, 97.5, 67.3, 63.4, 47.7, 33, 142, 0, 35.8, 17.9, 66.1,
25.6, 0.6, 246.8, 109.1, 122.4, 89.7, 2.6, 44.6, 8, 91, 60.2,
100.8, 99.8, 59.2, 112.9, 10.2, 66.5, 0.8, 0.8, 64.5, 131.4,
173, 50.4, 7.3, 7.3, 7.3, 590), `2012` = c(8, 98.2, 81.1, 40.7,
48.9, 61.8, 33.5, 33.5, 27.1, 73.7, 80.9, 80.9, 82.1, 80.9, 80.9,
80.9, 82.1, 82, 75.9, 78.3, 75.5, 99.3, 97.2, 75.5, 75.5, 75.5,
44.2, 240.8, 46.8, 44.7, 51.4, 45.2, 46, 2.6, 43.8, 23.5, 10.6,
21.1, 40.1, 91.6, 0, 23.5, 18.6, 34.8, 48, 36.2, 124.6, 153.4,
146.6, 87.2, 12.4, 98, 60.6, 69.1, 118.4, 115.1, 43.6, 108.1,
117.8, 0, 5.8, 13.9, 13.9, 57.4, 79, 165, 67.5, 60, 60, 60, 357
), `2012` = c(9, 23.9, 38.9, 53.2, 60.9, 44.2, 17.3, 17.3, 17.4,
54.2, 45.6, 45.6, 43.6, 45.6, 45.6, 45.6, 43.6, 44.8, 30.8, 27.2,
27.2, 79.2, 84.3, 27.2, 27.2, 27.2, 95.1, 125.9, 42.7, 30.8,
27.4, 43.1, 35.5, 49.2, 64.4, 22.5, 38.6, 60.6, 67.1, 20.3, 0,
69, 9, 131.8, 56.1, 58.2, 272.8, 163.3, 198.6, 50.9, 67.1, 75.6,
85.3, 72.4, 63.5, 126.9, 63.2, 118.7, 112, 0.2, 32.8, 10.3, 10.3,
48.2, 30.2, 169, 125.8, 87, 87, 87, 512.9), `2012` = c(10, 81.6,
29.2, 112.8, 119.8, 29.1, 330.5, 330.5, 295.4, 111, 60.3, 60.3,
54.9, 60.3, 60.3, 60.3, 54.9, 43.8, 53.6, 63.8, 67.7, 84.4, 87.8,
67.7, 67.7, 67.7, 166.6, 47.3, 38.3, 39.2, 44.2, 33, 46.6, 40.6,
92.5, 60.3, 84.4, 122.7, 184.9, 24.1, 2.1, 48.4, 22.5, 81.8,
111.3, 148.3, 119.7, 120, 142.3, 91.8, 150.6, 108.9, 55.9, 65.6,
69.2, 135.9, 110.7, 114.6, 100, 103.4, 28.2, 38.4, 38.4, 127.2,
87, 203, 172, 53.8, 53.8, 53.8, 258.7), `2012` = c(11, 92.3,
60.8, 41.8, 68.3, 43.6, 284.6, 284.6, 276.4, 45.9, 23.7, 23.7,
21.4, 23.7, 23.7, 23.7, 21.4, 17.1, 27.2, 26.5, 26.5, 33.4, 36.8,
26.5, 26.5, 26.5, 31.8, 43.9, 67.4, 46.6, 69.8, 62.5, 40.3, 68.2,
38.8, 54.6, 39.2, 81.9, 66.7, 73.1, 56.9, 177.4, 194.8, 136.5,
184.8, 183.7, 126.6, 158.5, 200.9, 37.2, 238.5, 84.3, 51.5, 48,
56.3, 103.3, 101, 20.8, 39.4, 161.3, 2.6, 9, 9, 37.6, 33.5, 223,
120.2, 49.6, 49.6, 49.6, 54.7), `2012` = c(12, 34, 36.5, 156.7,
195.4, 132.6, 310.4, 310.4, 271.8, 87.6, 15.3, 15.3, 13.2, 15.3,
15.3, 15.3, 13.2, 10.3, 17.5, 15.8, 14.8, 77.5, 74.5, 14.8, 14.8,
14.8, 122.4, 5.1, 81.1, 74.5, 58.7, 69.1, 55.8, 3.3, 48.1, 33.3,
83.9, 173.5, 30.4, 169.4, 41.7, 86.6, 67.9, 36.3, 58.8, 121.9,
100.7, 163.3, 155.5, 135.4, 255.8, 60, 18.7, 67.9, 57.8, 150.3,
136.2, 109.2, 123.3, 179.5, 35.3, 92.4, 92.4, 71.4, 50.5, 237,
135.4, 16.5, 16.5, 16.5, 1.1)), .Names = c("year", "2012", "2012",
"2012", "2012", "2012", "2012", "2012", "2012", "2012", "2012",
"2012", "2012"), row.names = c(1L, 3L, 4L, 5L, 6L, 7L, 8L, 9L,
10L, 11L, 12L, 13L, 14L, 15L, 16L, 17L, 18L, 19L, 20L, 21L, 22L,
23L, 24L, 25L, 26L, 27L, 28L, 29L, 30L, 31L, 32L, 33L, 34L, 35L,
36L, 37L, 38L, 39L, 40L, 41L, 42L, 43L, 44L, 45L, 46L, 47L, 48L,
49L, 50L, 51L, 52L, 53L, 54L, 55L, 56L, 57L, 58L, 59L, 60L, 61L,
62L, 63L, 64L, 65L, 66L, 67L, 68L, 69L, 70L, 71L, 72L), class = "data.frame")
I want to transform my dataframe using the R package dplyr. So far, year and month are in rows but I want them in columns. Basically I want a dataframe with a column containing the IDs, one with year, one with month and one with the values.
The structure will look like this with an ID column additional.
df2<-structure(list(YEAR = c(1980L, 1980L, 1980L, 1980L, 1980L, 1980L,
1980L, 1980L, 1980L, 1980L), MONTH = 1:10, PRCP = c(46.3, 20.7,
101.3, 27.2, 67.5, 34.1, 12, 95.4, 17, 31.8)), .Names = c("YEAR",
"MONTH", "PRCP"), row.names = c(NA, 10L), class = "data.frame")
Can someone help me out? Thanks
Here is the code using dplyr / tidyr combination and magrittr (for convenience).
Vectorize(require)(package = c("tidyr", # Data manipulation
"dplyr", # Data manipulation
"magrittr" # Reverse pipe
),
char = TRUE)
dfTr <- df %>%
gather(key = key, value = value, convert = TRUE, -year) %>%
setNames(make.names(names(.), unique = TRUE)) %>%
select(year, key, value)
You can ad the following syntax to obtain one id column:
# Unite is optional as it will generate one ID column
unite(year_month, key, year)
which would produce:
year key value
1 month 2012 1.0
2 id.1.00000 2012 76.1
3 id.2.00000 2012 38.1
With exclusion
Sample approach excluding that month row:
dfTr2 <- df %>%
setNames(make.names(names(.), unique = TRUE)) %>%
filter(year != "month") %>%
gather(key = key, value = value, convert = TRUE, -year) %>%
select(year, key, value) %>%
unite(year_month, key, year) %>%
mutate(year_month = gsub("X","",year_month))
Cleaning month value
It crossed my mind that you may want to use the month figure, a small example below extracts the relevant digit:
library(stringr)
dfTr3 <- df %>%
setNames(make.names(names(.), unique = TRUE)) %>%
filter(year != "month") %>%
gather(key = key, value = value, convert = TRUE, -year) %>%
select(year, key, value) %>%
unite(year_month, key, year) %>%
mutate(year_month = gsub("X","",year_month)) %>%
mutate(month = gsub(".", "", str_extract_all(string = year_month,
pattern = "\\.\\d{1,2}\\."),
fixed = TRUE))
which produces:
> head(dfTr3, n = 2)
year_month value month
1 2012_id.1.00000 76.1 1
2 2012_id.2.00000 38.1 2
Your data is in an odd form; it appears that you have two header rows, which is not something supported in an R data frame. Most likely there was something wrong with the initial import, but I can see what it is you are trying to do.
My solution would be to first fix up the data frame so that it shows what we want, which is for the headers to be the combination of the current headers (year) and the first row (month)
> paste(names(df), df[1,])
[1] "year 72" "2012 1" "2012 2" "2012 3" "2012 4" "2012 5" "2012 6"
[8] "2012 7" "2012 8" "2012 9" "2012 10" "2012 11" "2012 12"
That looks good (except for "year 72", which we'll fix up later), so lets set it:
> names(df) <- paste(names(df), df[1,])
> df %>% head
year 72 2012 1 2012 2 2012 3 2012 4 2012 5 2012 6 2012 7 2012 8 2012 9
1 month 1.0 2.0 3.0 4.0 5.0 6.0 7.0 8.0 9.0
3 id.1.00000 76.1 104.6 196.1 27.5 72.5 101.8 62.8 98.2 23.9
4 id.2.00000 38.1 41.4 75.8 45.4 102.2 124.8 68.9 81.1 38.9
Now let's get rid of that first row
> df <- tail(df, -1)
> head(df)
year 72 2012 1 2012 2 2012 3 2012 4 2012 5 2012 6 2012 7 2012 8 2012 9
3 id.1.00000 76.1 104.6 196.1 27.5 72.5 101.8 62.8 98.2 23.9
4 id.2.00000 38.1 41.4 75.8 45.4 102.2 124.8 68.9 81.1 38.9
Now we can use tidyr, a companion package to dplyr for reshaping data frames, to put it into the desired form.
> require(tidyr)
> df %>% gather(date, value, -`year 72`) %>% head
year 72 date value
1 id.1.00000 2012 1 76.1
2 id.2.00000 2012 1 38.1
3 id.3.00000 2012 1 88.6
4 id.4.00000 2012 1 132.5
5 id.5.00000 2012 1 241.9
From here, we can use separate to break the "date" column into "year" and "month" columns.
This should do the trick!
rownames(df) <- seq(length=nrow(df))
colnames(df)[2:13] <- c(1:12)
tmp <- melt(df[2:nrow(df),],id = c('year'))
colnames(tmp) <- c('id','Month','Value')
library(sqldf)
sqldf("Select * from tmp order by id")
I have to find the K Shortest Path,However the below code i tried gives the same path when i choose different K Values and the distance computed is not correct.
My dataset is my.graph with class igraph
dput(my.graph)
structure(list(169, FALSE, c(22, 1, 2, 1, 2, 3, 114, 3, 4, 5,
4, 5, 6, 6, 7, 7, 8, 9, 8, 110, 78, 159, 9, 159, 30, 11, 13,
160, 11, 66, 160, 138, 14, 13, 14, 15, 81, 16, 15, 17, 16, 17,
18, 18, 19, 130, 19, 62, 62, 23, 42, 22, 22, 22, 23, 24, 161,
24, 25, 25, 26, 64, 26, 28, 161, 29, 28, 29, 47, 48, 53, 142,
31, 30, 32, 31, 32, 33, 33, 34, 35, 118, 34, 36, 35, 37, 36,
37, 38, 39, 38, 162, 40, 39, 40, 41, 41, 42, 43, 44, 43, 44,
45, 45, 46, 47, 46, 47, 47, 49, 48, 49, 50, 51, 50, 52, 51, 52,
53, 60, 53, 54, 53, 55, 54, 56, 55, 57, 56, 57, 58, 58, 59, 59,
60, 60, 60, 63, 162, 62, 62, 63, 64, 65, 65, 66, 166, 68, 163,
164, 69, 165, 68, 70, 69, 71, 70, 71, 72, 72, 73, 112, 73, 74,
75, 74, 76, 75, 76, 77, 78, 77, 78, 110, 78, 79, 80, 79, 146,
80, 81, 82, 81, 81, 82, 137, 164, 84, 85, 84, 86, 85, 86, 87,
87, 164, 165, 89, 89, 90, 90, 91, 92, 91, 93, 92, 93, 94, 95,
94, 165, 95, 163, 97, 97, 98, 99, 98, 99, 100, 101, 100, 101,
102, 102, 163, 104, 166, 105, 104, 106, 105, 106, 107, 108, 107,
109, 108, 109, 166, 110, 110, 125, 116, 112, 113, 112, 112, 114,
113, 114, 115, 114, 126, 115, 116, 117, 118, 117, 119, 118, 118,
120, 119, 120, 121, 121, 122, 123, 122, 124, 168, 141, 123, 124,
125, 125, 125, 126, 140, 140, 128, 128, 129, 130, 129, 130, 130,
131, 131, 132, 133, 132, 134, 133, 134, 135, 135, 136, 137, 136,
137, 137, 139, 138, 139, 168, 143, 140, 140, 141, 142, 158, 167,
143, 167, 144, 145, 144, 145, 146, 146, 146, 148, 148, 149, 149,
150, 151, 150, 152, 151, 153, 152, 153, 154, 154, 155, 156, 155,
156, 157, 157, 158, 158, 158, 159, 160, 159, 160, 160, 160, 161,
161, 162, 162, 163, 163, 163, 164, 164, 164, 165, 165, 165, 166,
166, 166, 167, 167, 168, 168), c(0, 0, 1, 0, 1, 2, 2, 2, 3, 4,
3, 4, 5, 5, 6, 6, 7, 8, 7, 9, 9, 9, 8, 10, 10, 10, 11, 11, 10,
12, 12, 12, 13, 11, 13, 14, 14, 15, 14, 16, 15, 16, 17, 17, 18,
19, 18, 19, 20, 20, 21, 21, 0, 21, 20, 23, 23, 23, 24, 24, 25,
26, 25, 27, 27, 28, 27, 28, 29, 29, 29, 30, 30, 10, 31, 30, 31,
32, 32, 33, 34, 34, 33, 35, 34, 36, 35, 36, 37, 38, 37, 38, 39,
38, 39, 40, 40, 21, 42, 43, 42, 43, 44, 44, 45, 46, 45, 29, 46,
48, 29, 48, 49, 50, 49, 51, 50, 51, 52, 53, 52, 53, 29, 54, 53,
55, 54, 56, 55, 56, 57, 57, 58, 58, 59, 53, 59, 61, 61, 20, 19,
61, 26, 64, 64, 12, 67, 67, 67, 68, 68, 68, 67, 69, 68, 70, 69,
70, 71, 71, 72, 72, 72, 73, 74, 73, 75, 74, 75, 76, 77, 76, 77,
78, 9, 78, 79, 78, 80, 79, 80, 81, 80, 14, 81, 82, 83, 83, 84,
83, 85, 84, 85, 86, 86, 87, 88, 88, 88, 89, 89, 90, 91, 90, 92,
91, 92, 93, 94, 93, 95, 94, 96, 96, 96, 97, 98, 97, 98, 99, 100,
99, 100, 101, 101, 102, 103, 103, 104, 103, 105, 104, 105, 106,
107, 106, 108, 107, 108, 109, 9, 78, 110, 111, 111, 112, 72,
111, 113, 112, 113, 114, 2, 115, 114, 111, 116, 117, 116, 118,
117, 34, 119, 118, 119, 120, 120, 121, 122, 121, 123, 123, 123,
122, 123, 124, 124, 110, 115, 126, 127, 127, 127, 128, 129, 128,
129, 19, 130, 130, 131, 132, 131, 133, 132, 133, 134, 134, 135,
136, 135, 136, 82, 138, 12, 138, 139, 139, 127, 126, 123, 30,
142, 142, 139, 143, 143, 144, 143, 144, 145, 80, 145, 147, 147,
148, 148, 149, 150, 149, 151, 150, 152, 151, 152, 153, 153, 154,
155, 154, 155, 156, 156, 157, 142, 157, 9, 159, 10, 12, 11, 159,
23, 27, 61, 38, 96, 67, 102, 68, 83, 87, 95, 88, 68, 67, 109,
103, 142, 143, 123, 139), c(3, 1, 4, 2, 7, 5, 10, 8, 11, 9, 13,
12, 15, 14, 18, 16, 22, 17, 28, 25, 33, 26, 34, 32, 38, 35, 40,
37, 41, 39, 43, 42, 46, 44, 52, 0, 53, 51, 54, 49, 57, 55, 59,
58, 62, 60, 66, 63, 67, 65, 73, 24, 75, 72, 76, 74, 78, 77, 82,
79, 84, 80, 86, 83, 87, 85, 90, 88, 93, 89, 94, 92, 96, 95, 97,
50, 100, 98, 101, 99, 103, 102, 106, 104, 107, 68, 108, 105,
110, 69, 111, 109, 114, 112, 116, 113, 117, 115, 122, 70, 120,
118, 124, 121, 126, 123, 128, 125, 129, 127, 131, 130, 133, 132,
135, 119, 136, 134, 140, 47, 139, 48, 141, 137, 142, 61, 144,
143, 145, 29, 152, 147, 154, 150, 156, 153, 157, 155, 159, 158,
162, 160, 165, 163, 167, 164, 168, 166, 171, 169, 174, 20, 172,
170, 177, 175, 179, 176, 183, 36, 182, 180, 184, 181, 189, 187,
191, 188, 192, 190, 194, 193, 198, 197, 200, 199, 203, 201, 205,
202, 206, 204, 209, 207, 211, 208, 214, 213, 217, 215, 218, 216,
221, 219, 222, 220, 224, 223, 229, 226, 231, 228, 232, 230, 235,
233, 237, 234, 238, 236, 240, 19, 241, 173, 246, 161, 247, 244,
249, 245, 252, 6, 250, 248, 254, 251, 255, 243, 258, 256, 261,
81, 260, 257, 263, 259, 264, 262, 266, 265, 269, 267, 273, 268,
274, 270, 277, 242, 276, 275, 278, 253, 282, 281, 285, 283, 287,
45, 286, 284, 289, 288, 292, 290, 294, 291, 295, 293, 297, 296,
300, 298, 302, 185, 301, 299, 304, 31, 305, 303, 309, 279, 308,
280, 310, 272, 311, 71, 314, 307, 318, 316, 319, 317, 321, 178,
322, 320, 324, 323, 326, 325, 329, 327, 331, 328, 333, 330, 334,
332, 336, 335, 339, 337, 340, 338, 342, 341, 344, 312, 345, 343,
346, 21, 348, 23, 350, 27, 349, 30, 351, 347, 352, 56, 353, 64,
355, 91, 354, 138, 357, 148, 356, 212, 358, 225, 359, 149, 360,
186, 361, 195, 364, 151, 363, 196, 362, 210, 365, 146, 367, 227,
366, 239, 368, 313, 369, 315, 370, 271, 371, 306), c(3, 1, 52,
0, 4, 2, 7, 5, 252, 6, 10, 8, 11, 9, 13, 12, 15, 14, 18, 16,
22, 17, 174, 20, 240, 19, 346, 21, 28, 25, 73, 24, 348, 23, 33,
26, 350, 27, 145, 29, 304, 31, 349, 30, 34, 32, 38, 35, 183,
36, 40, 37, 41, 39, 43, 42, 46, 44, 140, 47, 287, 45, 54, 49,
139, 48, 53, 51, 97, 50, 57, 55, 352, 56, 59, 58, 62, 60, 142,
61, 66, 63, 353, 64, 67, 65, 107, 68, 110, 69, 122, 70, 75, 72,
311, 71, 76, 74, 78, 77, 82, 79, 84, 80, 261, 81, 86, 83, 87,
85, 90, 88, 93, 89, 355, 91, 94, 92, 96, 95, 100, 98, 101, 99,
103, 102, 106, 104, 108, 105, 111, 109, 114, 112, 116, 113, 117,
115, 120, 118, 124, 121, 135, 119, 126, 123, 128, 125, 129, 127,
131, 130, 133, 132, 136, 134, 141, 137, 354, 138, 144, 143, 152,
147, 357, 148, 365, 146, 154, 150, 359, 149, 364, 151, 156, 153,
157, 155, 159, 158, 162, 160, 246, 161, 165, 163, 167, 164, 168,
166, 171, 169, 172, 170, 177, 175, 241, 173, 179, 176, 182, 180,
321, 178, 184, 181, 302, 185, 189, 187, 360, 186, 191, 188, 192,
190, 194, 193, 361, 195, 198, 197, 363, 196, 200, 199, 203, 201,
205, 202, 206, 204, 209, 207, 211, 208, 362, 210, 214, 213, 356,
212, 217, 215, 218, 216, 221, 219, 222, 220, 224, 223, 358, 225,
229, 226, 367, 227, 231, 228, 232, 230, 235, 233, 237, 234, 238,
236, 366, 239, 277, 242, 247, 244, 255, 243, 249, 245, 250, 248,
254, 251, 278, 253, 258, 256, 260, 257, 263, 259, 264, 262, 266,
265, 269, 267, 273, 268, 274, 270, 310, 272, 370, 271, 276, 275,
309, 279, 282, 281, 308, 280, 285, 283, 286, 284, 289, 288, 292,
290, 294, 291, 295, 293, 297, 296, 300, 298, 301, 299, 305, 303,
314, 307, 371, 306, 344, 312, 368, 313, 318, 316, 369, 315, 319,
317, 322, 320, 324, 323, 326, 325, 329, 327, 331, 328, 333, 330,
334, 332, 336, 335, 339, 337, 340, 338, 342, 341, 345, 343, 351,
347), c(0, 0, 2, 4, 6, 8, 10, 12, 14, 16, 18, 18, 20, 20, 22,
24, 26, 28, 30, 32, 34, 34, 34, 38, 40, 42, 44, 46, 46, 48, 50,
52, 54, 56, 58, 60, 62, 64, 66, 68, 70, 72, 74, 76, 78, 80, 82,
84, 88, 90, 92, 94, 96, 98, 102, 104, 106, 108, 110, 112, 114,
118, 118, 122, 124, 126, 128, 130, 130, 132, 134, 136, 138, 140,
142, 144, 146, 148, 150, 154, 156, 158, 162, 164, 164, 166, 168,
170, 172, 172, 174, 176, 178, 180, 182, 184, 186, 186, 188, 190,
192, 194, 196, 198, 198, 200, 202, 204, 206, 208, 210, 214, 214,
218, 220, 224, 226, 228, 230, 234, 236, 238, 240, 242, 244, 246,
250, 252, 252, 254, 256, 260, 262, 264, 266, 268, 270, 272, 276,
278, 280, 284, 286, 288, 290, 292, 294, 298, 298, 300, 302, 304,
306, 308, 310, 312, 314, 316, 318, 322, 326, 332, 336, 340, 346,
352, 358, 364, 368, 372), c(0, 4, 6, 10, 12, 14, 16, 18, 20,
22, 28, 34, 38, 44, 46, 50, 52, 54, 56, 58, 62, 66, 70, 70, 74,
76, 78, 80, 84, 86, 92, 96, 98, 100, 102, 106, 108, 110, 112,
116, 118, 120, 120, 122, 124, 126, 128, 130, 130, 132, 134, 136,
138, 140, 144, 146, 148, 150, 152, 154, 156, 156, 160, 160, 160,
162, 162, 162, 168, 174, 176, 178, 180, 184, 186, 188, 190, 192,
194, 198, 200, 204, 206, 208, 212, 214, 216, 218, 220, 224, 226,
228, 230, 232, 234, 236, 238, 242, 244, 246, 248, 250, 252, 254,
258, 260, 262, 264, 266, 268, 270, 272, 276, 278, 280, 282, 284,
286, 288, 290, 292, 294, 296, 298, 304, 306, 306, 308, 312, 314,
316, 318, 320, 322, 324, 326, 328, 330, 330, 332, 336, 336, 336,
340, 344, 346, 348, 348, 350, 352, 354, 356, 358, 360, 362, 364,
366, 368, 370, 370, 372, 372, 372, 372, 372, 372, 372, 372, 372,
372), list(c(1, 0, 1), structure(list(), .Names = character(0)),
structure(list(name = c("1", "2", "3", "4", "5", "6", "7",
"8", "9", "10", "11", "12", "13", "14", "15", "16", "17",
"18", "19", "20", "21", "22", "23", "24", "25", "26", "27",
"28", "29", "30", "31", "32", "33", "34", "35", "36", "37",
"38", "39", "40", "41", "42", "43", "44", "45", "46", "47",
"48", "49", "50", "51", "52", "53", "54", "55", "56", "57",
"58", "59", "60", "61", "62", "63", "64", "65", "66", "67",
"68", "69", "70", "71", "72", "73", "74", "75", "76", "77",
"78", "79", "80", "81", "82", "83", "84", "85", "86", "87",
"88", "89", "90", "91", "92", "93", "94", "95", "96", "97",
"98", "99", "100", "101", "102", "103", "104", "105", "106",
"107", "108", "109", "110", "111", "112", "113", "114", "115",
"116", "117", "118", "119", "120", "121", "122", "123", "124",
"125", "126", "127", "128", "129", "130", "131", "132", "133",
"134", "135", "136", "137", "138", "139", "140", "141", "142",
"143", "144", "145", "146", "147", "148", "149", "150", "151",
"152", "153", "154", "155", "156", "157", "158", "159", "160",
"161", "162", "163", "164", "165", "166", "167", "168", "169"
)), .Names = "name"), structure(list(DIST_KM_CNT = c(4.89,
1.45, 2.36, 1.45, 2.36, 1.18, 0, 1.18, 0.89, 1.47, 0.89,
1.47, 1.16, 1.16, 1.2, 1.2, 1.02, 0.79, 1.02, 0, 0, 1, 0.79,
0, 0.98, 1.03, 1.15, 0, 1.03, 1.35, 0.95, 0, 0.99, 1.15,
0.99, 1.53, 0, 1.22, 1.53, 1.37, 1.22, 1.37, 1.23, 1.23,
1.1, 0, 1.1, 1.38, 1.69, 3.49, 3.16, 1.38, 4.89, 1.38, 3.49,
1.51, 0, 1.51, 1.39, 1.39, 1.78, 0.947, 1.78, 1.17, 2.12,
3.26, 1.17, 3.26, 1.43, 0, 0, 15.58, 1.11, 0.98, 1.09, 1.11,
1.09, 1.43, 1.43, 1.15, 1.11, 0, 1.15, 1.13, 1.11, 1.96,
1.13, 1.96, 1.86, 2.48, 1.86, 0, 1.44, 2.48, 1.44, 2.38,
2.38, 3.16, 2.41, 1.691, 2.41, 1.691, 1.54, 1.54, 1.65, 4.14,
1.65, 1.43, 4.14, 0.572, 0, 0.572, 0.455, 0.558, 0.455, 0.54,
0.558, 0.54, 0.682, 0.638, 0.682, 0.42, 0, 0.624, 0.42, 0.47,
0.624, 0.895, 0.47, 0.895, 0.493, 0.493, 0.703, 0.703, 0.553,
0.638, 0.553, 4.52, 1.94, 1.69, 1.38, 4.52, 0.947, 2.647,
2.647, 1.35, 0, 1.66, 0, 0, 1.05, 0, 1.66, 1.31, 1.05, 1.54,
1.31, 1.54, 1.72, 1.72, 1.24, 0, 1.24, 0.94, 1.57, 0.94,
1.15, 1.57, 1.15, 0.77, 0.95, 0.77, 0.95, 0, 0, 1.38, 0.6,
1.38, 11.42, 0.6, 0.72, 2.64, 0.72, 0, 2.64, 0, 0.82, 0.708,
0.467, 0.708, 0.59, 0.467, 0.59, 0.828, 0.828, 1.047, 0.77,
0.517, 0.517, 0.897, 0.897, 0.727, 0.602, 0.727, 0.481, 0.602,
0.481, 0.726, 0.602, 0.726, 0.92, 0.602, 0.986, 0.44, 0.44,
0.513, 0.548, 0.513, 0.548, 0.721, 0.513, 0.721, 0.513, 0.564,
0.564, 0.937, 0.412, 0.576, 0.542, 0.412, 0.567, 0.542, 0.567,
0.497, 0.426, 0.497, 0.379, 0.426, 0.379, 0.987, 0, 0, 0.614,
1.321, 1.327, 0.912, 0, 1.327, 1.735, 0.912, 1.735, 1.577,
0, 1.188, 1.577, 1.321, 1.017, 1.057, 1.017, 1.239, 1.057,
0, 0.732, 1.239, 0.732, 0.877, 0.877, 1.548, 0.816, 1.548,
0.806, 0, 11.5, 0.816, 0.806, 0.689, 0.689, 0.614, 1.188,
1.357, 2.496, 1.028, 1.028, 1.432, 0.93, 1.432, 0.93, 0,
0.794, 0.794, 0.811, 1.395, 0.811, 1.323, 1.395, 1.323, 1.385,
1.385, 0.774, 1.53, 0.774, 1.53, 0, 0.841, 0, 0.841, 1.317,
7.75, 2.496, 1.357, 11.5, 15.58, 0.75, 0.905, 7.75, 1.317,
0.89, 0.593, 0.89, 0.593, 0.555, 11.42, 0.555, 1.18, 1.18,
0.87, 0.87, 2.63, 1.21, 2.63, 1.6, 1.21, 1.26, 1.6, 1.26,
1.09, 1.09, 1.12, 1.58, 1.12, 1.58, 1.42, 1.42, 0.54, 0.75,
0.54, 1, 1.03, 0, 0.95, 0, 1.03, 0, 2.12, 1.94, 0, 0.986,
0, 0.937, 0, 0.82, 1.047, 0.92, 0.77, 0, 0, 0.987, 0.576,
0.905, 1.317, 0, 1.317)), .Names = "DIST_KM_CNT")), <environment>), class = "igraph")
K Shortest Path logic
# find k shortest paths
k.shortest.paths <- function(graph, from, to, k){
# first shortest path
k0 <- get.shortest.paths(graph,from,to, output='both')
# number of currently found shortest paths
kk <- 1
# list of alternatives
variants <- list()
# shortest variants
shortest.variants <- list(list(g=graph, path=k0$epath, vert=k0$vpath, dist=shortest.paths(graph,from,to)))
# until k shortest paths are found
while(kk<k){
# take last found shortest path
last.variant <- shortest.variants[[length(shortest.variants)]]
# calculate all alternatives
variants <- calculate.variants(variants, last.variant, from, to)
# find shortest alternative
sp <- select.shortest.path(variants)
# add to list, increase kk, remove shortest path from list of alternatives
shortest.variants[[length(shortest.variants)+1]] <- list(g=variants[[sp]]$g, path=variants[[sp]]$variants$path, vert=variants[[sp]]$variants$vert, dist=variants[[sp]]$variants$dist)
kk <- kk+1
variants <- variants[-sp]
}
return(shortest.variants)
}
# found all alternative routes
calculate.variants <- function(variants, variant, from, to){
# take graph from current path
g <- variant$g
# iterate through edges, removing one each iterations
for (j in unlist(variant$path)){
newgraph <- delete.edges(g, j) # remove adge
sp <- get.shortest.paths(newgraph,from,to, output='both') # calculate shortest path
spd <- shortest.paths(newgraph,from,to) # calculate length
if (spd != Inf){ # the the path is found
if (!contains.path(variants, sp$vpath)) # add to list, unless it already contains the same path
{
variants[[length(variants)+1]] <- list(g=newgraph, variants=list(path=sp$epath, vert=sp$vpath, dist=spd))
}
}
}
return(variants)
}
# does a list contain this path?
contains.path <- function(variants, variant){
return( any( unlist( lapply( variants, function(x){ identical(x$variant$vert,variant) } ) ) ) )
}
# which path from the list is the shortest?
select.shortest.path <- function(variants){
return( which.min( unlist( lapply( variants, function(x){x$variants$dist} ) ) ) )
}
The results are below with Same Path and and the distance computed is also not correct.I am not sure about where i am making the mistake
library(igraph)
k.shortest.paths(my.graph, from = 37, to = 8, k = 2)
[[1]]
[[1]]$g
IGRAPH UN-- 169 372 --
+ attr: name (v/c), DIST_KM_CNT (e/n)
+ edges (vertex names):
[1] 1 --23 1 --2 2 --3 1 --2 2 --3 3 --4 3 --115 3 --4 4 --5
[10] 5 --6 4 --5 5 --6 6 --7 6 --7 7 --8 7 --8 8 --9 9 --10
[19] 8 --9 10--111 10--79 10--160 9 --10 11--160 11--31 11--12 12--14
[28] 12--161 11--12 13--67 13--161 13--139 14--15 12--14 14--15 15--16
[37] 15--82 16--17 15--16 17--18 16--17 17--18 18--19 18--19 19--20
[46] 20--131 19--20 20--63 21--63 21--24 22--43 22--23 1 --23 22--23
[55] 21--24 24--25 24--162 24--25 25--26 25--26 26--27 27--65 26--27
[64] 28--29 28--162 29--30 28--29 29--30 30--48 30--49 30--54 31--143
+ ... omitted several edges
[[1]]$path
[[1]]$path[[1]]
+ 11/372 edges (vertex names):
[1] 36--37 35--36 34--35 33--34 32--33 31--32 11--31 11--160 10--160
[10] 9 --10 8 --9
[[1]]$vert
[[1]]$vert[[1]]
+ 12/169 vertices, named:
[1] 37 36 35 34 33 32 31 11 160 10 9 8
[[1]]$dist
8
37 11
[[2]]
[[2]]$g
IGRAPH UN-- 169 371 --
+ attr: name (v/c), DIST_KM_CNT (e/n)
+ edges (vertex names):
[1] 1 --23 1 --2 2 --3 1 --2 2 --3 3 --4 3 --115 3 --4 4 --5
[10] 5 --6 4 --5 5 --6 6 --7 6 --7 7 --8 7 --8 8 --9 9 --10
[19] 8 --9 10--111 10--79 10--160 9 --10 11--160 11--31 11--12 12--14
[28] 12--161 11--12 13--67 13--161 13--139 14--15 12--14 14--15 15--16
[37] 15--82 16--17 15--16 17--18 16--17 17--18 18--19 18--19 19--20
[46] 20--131 19--20 20--63 21--63 21--24 22--43 22--23 1 --23 22--23
[55] 21--24 24--25 24--162 24--25 25--26 25--26 26--27 27--65 26--27
[64] 28--29 28--162 29--30 28--29 29--30 30--48 30--49 30--54 31--143
+ ... omitted several edges
[[2]]$path
[[2]]$path[[1]]
+ 11/371 edges (vertex names):
[1] 36--37 35--36 34--35 33--34 32--33 31--32 11--31 11--160 10--160
[10] 9 --10 8 --9
[[2]]$vert
[[2]]$vert[[1]]
+ 12/169 vertices, named:
[1] 37 36 35 34 33 32 31 11 160 10 9 8
[[2]]$dist
8
37 11
I know this is like 2 years late but hopefully this will be useful for other people who needs an implementation of yen's algorithm in R.
library(igraph)
library(tidyverse)
#'#return the shortest path as a list of vertices or NULL if there is no path between src and dest
shortest_path <- function(graph, src, dest){
path <- suppressWarnings(get.shortest.paths(graph, src, dest))
path <- names(path$vpath[[1]])
if (length(path)==1) NULL else path
}
#'#return the sum of the weights of all the edges in the given path
path_weight <- function(path, graph) sum(E(graph, path=path)$weight)
#'#description sorts a list of paths based on the weight of the path
sort_paths <- function(graph, paths) paths[paths %>% sapply(path_weight, graph) %>% order]
#'#description creates a list of edges that should be deleted
find_edges_to_delete <- function(A,i,rootPath){
edgesToDelete <- NULL
for (p in A){
rootPath_p <- p[1:i]
if (all(rootPath_p == rootPath)){
edge <- paste(p[i], ifelse(is.na(p[i+1]),p[i],p[i+1]), sep = '|')
edgesToDelete[length(edgesToDelete)+1] <- edge
}
}
unique(edgesToDelete)
}
#returns the k shortest path from src to dest
#sometimes it will return less than k shortest paths. This occurs when the max possible number of paths are less than k
k_shortest_yen <- function(graph, src, dest, k){
if (src == dest) stop('src and dest can not be the same (currently)')
#accepted paths
A <- list(shortest_path(graph, src, dest))
if (k == 1) return (A)
#potential paths
B <- list()
for (k_i in 2:k){
prev_path <- A[[k_i-1]]
num_nodes_to_loop <- length(prev_path)-1
for(i in 1:num_nodes_to_loop){
spurNode <- prev_path[i]
rootPath <- prev_path[1:i]
edgesToDelete <- find_edges_to_delete(A, i,rootPath)
t_g <- delete.edges(graph, edgesToDelete)
#for (edge in edgesToDelete) t_g <- delete.edges(t_g, edge)
spurPath <- shortest_path(t_g,spurNode, dest)
if (!is.null(spurPath)){
total_path <- list(c(rootPath[-i], spurPath))
if (!total_path %in% B) B[length(B)+1] <- total_path
}
}
if (length(B) == 0) break
B <- sort_paths(graph, B)
A[k_i] <- B[1]
B <- B[-1]
}
A
}
#===================Test==========================#
edgeList <- tibble(from=character(), to=character(), weight = numeric())
edgeList[nrow(edgeList)+1,] <-list('c','d',3)
edgeList[nrow(edgeList)+1,] <-list('d','f',4)
edgeList[nrow(edgeList)+1,] <-list('f','h',1)
edgeList[nrow(edgeList)+1,] <-list('c','e',2)
edgeList[nrow(edgeList)+1,] <-list('e','d',1)
edgeList[nrow(edgeList)+1,] <-list('e','f',2)
edgeList[nrow(edgeList)+1,] <-list('e','g',3)
edgeList[nrow(edgeList)+1,] <-list('g','h',2)
edgeList[nrow(edgeList)+1,] <-list('f','g',2)
graph <- graph.data.frame(edgeList)
#k_shortest.yen(graph, 'c','c',7) #expect error
#expect all 7 paths
k_shortest_yen(graph,'c','h',7)
I had the same problem and then i noticed that there are a error in the code. The function identical in function contains.path were not returning the correct value. I simply changed the code of identical(x$variant$vert,variant) to identical(unlist(x$variant$vert),unlist(variant)). And now the code is reporting all routings and no duplicates are present.