#r #for-loop #if-statement
#r #для-цикла #оператор if
Вопрос:
У меня есть очень простая задача, которая уже работала раньше, но теперь больше не работает. Я не могу найти ошибку! Я хочу добавить 2 столбца в свой фрейм данных, указав, есть ли дождь или нет дождя для V1 (без дождя = 0) и жаркий день или нет для V2 (жаркий день > = 30). Может кто-нибудь помочь, пожалуйста? Мой код прилагается.
for (a in 1:length(Test)){
if (Test$V2[a] >= 30){
Test$Heat[a] = "Hot"
} else {
Test$Heat[a] = "Not hot"
}
}
Вот мой код:
Test = structure(list(V1 = c(0, 1.8, 0, 0.3, 0, 0, 1.5, 1.6, 0, 0.2,
0.9, 7.4, 0.2, 0.3, 1.1, 0.6, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 2.6, 5, 0, 0, 1.5, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
3.2, 3.8, 0, 0.4, 2.1, 2.4, 0.1, 0, 0.1, 0, 0, 1.1, 4.7, 5.9,
0.6, 0, 0, 4.2, 1, 0, 4.8, 6.3, 0, 0, 0, 0, 0, 0, 0, 0.2, 3.2,
0, 0.1, 3.1, 1.6, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 6.4, 0,
0, 0, 0, 0, 0.2, 0, 0, 0, 0, 0.8, 2.7, 9.9, 3.1, 0.3, 0, 0, 1.9,
0, 0.5, 29.9, 8.6, 18.4, 4.8, 0, 0, 19.4, 0.5, 0.1, 11.8, 1,
1.6, 11.9, 16.4, 0, 0, 0.4, 1.9, 0.2, 5.1, 0, 0, 0, 0, 0.7, 0,
0, 0, 0, 0.6, 0, 0, 0, 0, 0, 2.4, 0, 0, 0, 4.5, 0.5, 1.1, 6,
5.2, 0, 6.9, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0.4, 2.5, 0, 0,
0.5, 4, 0.5, 24.5, 4.5, 2.1, 0.8, 1.9, 0, 0, 0, 0, 2.8, 3.1,
10.3, 3.6, 4.1, 2.4, 0, 3.6, 0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2 = c(-0.9, -0.1, 0, 0.9, -0.2,
-6.3, -4.9, -1.2, -0.3, -1.4, 2.7, 4.7, 5.4, 1.4, -0.5, -3, -4.8,
-3.9, -3.6, -3.7, -5, -8, -7.7, -7.4, -4, -2.2, -1.7, -1, 4.8,
4.5, 5.4, 2.9, 7.9, 7.2, 8.5, 8.4, 3.7, 5.5, 2.8, 0.8, -0.4,
5.8, 0.3, 3.7, 6.7, 11, 13.7, 5.6, 5.9, 4.4, 8.5, 9.6, 10.7,
18.9, 7.1, 7.9, 10.9, 14.7, 5.9, 7.3, 10.5, 10.8, 17.5, 10.8,
9.2, 7.3, 8.2, 10.2, 8.5, 10.4, 10.8, 10.5, 13.3, 12, 15.8, 19,
8.6, 13.7, 15, 13, 6.6, 12.4, 8.9, 13.4, 12.4, 16.5, 18.7, 18.1,
19.6, 22.6, 22.3, 18.2, 16.7, 13.9, 13.2, 8.6, 14, 18.3, 21.8,
22.4, 12.5, 18, 14.3, 13, 14.7, 8.7, 9.9, 5.7, 5.4, 6.3, 12.4,
10.8, 9.1, 16.8, 13.2, 4.5, 4.8, 6.1, 12.1, 15.3, 11.6, 13.8,
14.3, 14.8, 15.2, 20.3, 12.9, 11.7, 11.6, 17.9, 22, 21.2, 20.6,
19.3, 19.9, 22.1, 24.4, 26.3, 21.4, 17.8, 19.3, 22.9, 24.8, 18.6,
20.3, 23, 25.6, 28.8, 30.3, 28.4, 25, 25.6, 26.7, 28, 20.1, 20.2,
15.7, 15.3, 21.6, 24.8, 22.4, 27.1, 25.7, 24, 24.7, 29.1, 24.2,
20.8, 23.5, 28.5, 30.9, 30.8, 32.2, 29.9, 27.7, 25.1, 29.6, 27,
25.8, 22.6, 23.1, 21.5, 19.6, 22.3, 25.5, 29, 31.3, 30.2, 29.7,
28.5, 25.9, 22.8, 24.5, 21, 20.7, 18.4, 22.7, 26.5, 29.1, 31.9,
26.6, 29, 28.3, 22.2, 18.7, 16.8, 14.5, 20, 21.7, 26.8, 31.1,
28.6, 33.2, 29, 29.9, 26.1, 28, 20.9, 23.6, 26.7, 21.4, 17.3,
14.9, 18.8, 24, 25.9, 29.4, 24.6, 26.9, 30.2, 20.1, 19.5, 19.7,
21.8, 25.4, 26.5, 28.8, 29, 22.1, 25.2, 26.5, 30.7, 21.1, 14.3,
13.8, 17.1, 19.5, 22.9, 21.9, 17.2, 18.9, 16.3, 14.2, 18.5, 15.1,
18.9, 18.9, 16.3, 14.2, 16.3, 14.2, 12.7, 14.2, 16.5, 18.1, 19.1,
18.6, 17.6, 17.8, 20.7, 21, 17.6, 20, 14, 17.2, 15.5, 15.3, 19.6,
11.4, 12.6, 11.8, 11.7, 14.1, 18.2, 17.6, 17.9, 19.6, 20.5, 23.5,
23.3, 19.3, 20.5, 21.2, 18.8, 12, 9.5, 14.9, 17.9, 17.5, 12.2,
9.7, 9.4, 7.4, 8.4, 11.3, 14.3, 12.4, 15.6, 11.1, 6, 6.9, 6.9,
7.5, 6.7, 7.9, 9.9, 4.2, 3.5, 1.1, 0.9, 2.6, 5.2, 4.7, 4.5, 8.9,
14.9, 14.1, 15.7, 8.8, 2.6, 4.2, 4.6, 4, 1.3, 0.9, -0.5, -0.7,
2.1, 4.3, 2.9, 7.7, 7.3, -0.5, 5.5, 6.6, 5.1, 4.4, 6.2, 5.9,
2.5, 0.8, 0.1, 1.4, 2.4, 3.9, 5.8, 7.6, 4.9, 3.7, 9.1, 1.4, 1.5,
1.9, 10.7, 12.5, 8, 6.2, 11.4, 11, 10.9, 10, 5.3, 5.7, 10.2,
9.4, 3.2, 3.6, 3.2, 2.5, 4.5, 8.2, 3.1, 7.3, 4.9, 3.5, 1.3, 6.5,
6.9, 12.9, 13, 8.7, 5.7, 9.7, 9.3, 7.3, 10.2, 6.2, 4.8, 2.5,
2.7, 2.2, 3, -2.2, 1.9, 0.5, 2, 2.4, 2.5, 1.4, 1.7, 1.2, 5.4,
0, 0.3, -2.2, 0.4, -0.6, -1.3, -1.4, -0.8, -5.3, -8.7, -8.8,
-7.7, -3.8, -1.4, 2.4, 8.2, 8.3, 4.5, 9.8, 10.1, 12.4, 12.5,
13.8, 12.8, 9.9, 8.5, 11.1, 12, 3.3, -2, -3.9, 1.7, 3.2, 5, 6.5,
8.9, 12, 8.9, 9.6, 12.1, 9.8, 14.2, 9.4, 8.7, 15.1, 19.6, 22.1,
14.5, 13, 19.2, 20, 20.1, 18.3, 17.9, 21.5, 16.7, 18.9, 17.2,
16.8, 20.4, 21.9, 23.9, 25.6, 26.1, 26.3, 22.5, 20.5, 22.8, 13.4,
20.4, 22.1, 26, 19.6, 14.9, 18.2, 19.6, 21, 22.6, 22.7, 23.8,
23.6, 23.1, 20.9, 20.1, 25.2, 25.6, 21.1, 19.3, 14.1, 16.4, 19.5,
19.7, 20.9, 23.1, 23.9, 22.7, 21.6, 24, 24.4, 26.5, 27.5, 26.1,
25.9, 27.7, 23.8, 23.4, 26, 26.9, 27.1, 26.7, 26, 26.6, 26.9,
25.4, 29.4, 23.8, 15.8, 19.2, 20.8, 24.7, 23.1, 23.5, 24.9, 27.7,
28.1, 16.1, 18.3, 19.5, 20.8, 20.9, 22.8, 16.7, 24.6, 26.1, 22.3,
23.6, 26.4, 27.2, 25.1, 21.1, 24, 24.5, 26.7, 22.6, 19.2, 22.8,
26.9, 27.1, 27.1, 24.7, 26.9, 27.3, 27.8, 28.6, 20.1, 19.8, 25.6,
28.2, 30.3, 28.4, 29.5, 27.9, 29.1, 31.6, 33.2, 32.8, 30, 32.1,
33.5, 29.8, 30.8, 30.6, 29.2, 33.5, 20.3, 24.2, 28.9, 28.7, 25.1,
25, 27.7, 29.7, 27.3, 30, 29.4, 29.1, 31.7, 28.2, 22.9, 16.6,
18.3, 24.6, 25.1, 29.8, 20.4, 14.9, 13.7, 15.3, 19.6, 19.7, 23.7,
24.7, 20.8, 22.2, 24.5, 25.4, 27.2, 28.8, 25.4, 21.5, 20.8, 22.6,
26.1, 27.4, 26.6, 27.3, 27.9, 17.4, 24.7, 13.1, 12.2, 17.2, 21.9,
20.6, 15.5, 19.6, 12.8, 11.1, 13.3, 16.5, 19.9, 23, 21.3, 16.7,
16, 19.7, 23.6, 23.9, 22.7, 23.5, 22.7, 22.6, 20.3, 19, 17, 16.3,
14.3, 13.1, 12.7, 12.6, 13.7, 15.7, 7.3, 3, 11.6, 11.2, 10.4,
15.2, 10.7, 10.7, 9.7, 17, 14.9, 14.2, 12.8, 9.3, 8.7, 13.9,
12.1, 10.3, 5.7, 5.1, 5.7, 7.9, 5.1, 1.1, 5.7, 0.9, 2.8, 2.3,
8.9, 4.1, 3.2, 2.6, 0.3, 1.9, 5.3, 8.3, 10.3, 13.4, 9.7, 5.2,
10.1, 12.5, 7.5, 8.1, 4.9, 1.9, 1.8, -0.4, -1.6, -1.6, 0.4, 3.1,
5.9, 2.8, 7.5, 10, 10.9, 10.6, 10.8, 1.8, -1.6, -1, 4.7, 1.5,
4.4, 4.9)), .Names = c("V1", "V2"), class = "data.frame", row.names = c(NA,
-730L))
Ответ №1:
Мы можем использовать ifelse
вместо if/else
как ifelse
векторизовано, пока if/else
нет, и использование for
цикла было бы менее эффективным для такого рода задач в R
Test$Heat <- with(Test, ifelse(V2 >= 30, "Hot", "Not hot"))
Комментарии:
1. Вау, спасибо! Это намного проще. Но знаете ли вы, почему мой приведенный выше код не работает? Я не вижу никаких проблем с этим. Я обязательно буду использовать ваш код, но мне просто любопытно.
2. @Мистер Спок Да.
1:length
выполняется цикл по столбцам, потому чтоlength
в data.frame равно количеству столбцов. Вместо этого вы хотите перебирать строки1:nrow(Test)
Ответ №2:
Если вы измените функцию length() на функцию nrow(), вы сможете успешно выполнить приведенный выше код. Вот код.
for (a in 1:nrow(Test)){
if (Test$V2[a] >= 30){
Test$Heat[a] = "Hot"
} else {
Test$Heat[a] = "Not hot"
}
}