3

Sei que há outras perguntas sobre esse erro, mas como sou iniciante elas não me ajudaram a resolver o meu problema específico.

Estou trabalhando na seguinte data.frame:

structure(list(Dates = structure(c(16071, 16071, 16071, 16071, 
16071, 16071, 16071, 16071, 16071, 16071, 16071, 16071, 16071, 
16071, 16071, 16071, 16071, 16071, 16071, 16071, 16071, 16071, 
16071, 16071, 16071, 16071, 16071, 16071, 16071, 16071, 16071, 
16071, 16071, 16071, 16071, 16071, 16071, 16071, 16071, 16071, 
16071, 16071, 16071, 16071, 16071, 16071, 16071, 16071, 16071, 
16071, 16071, 16071, 16071, 16071, 16071, 16071, 16071, 16071, 
16071, 16071, 16071, 16071, 16071, 16071, 16071, 16071, 16071, 
16071, 16071, 16071, 16071, 16071, 16071, 16071, 16071, 16071, 
16071, 16071, 16071, 16071, 16071, 16071, 16071, 16071, 16071, 
16071, 16071, 16071, 16071, 16071, 16072, 16072, 16072, 16072, 
16072, 16072, 16072, 16072, 16072, 16072), class = "Date"), Identification = structure(c(1L, 
2L, 3L, 4L, 5L, 6L, 8L, 9L, 10L, 11L, 12L, 13L, 14L, 15L, 16L, 
17L, 18L, 19L, 20L, 21L, 22L, 23L, 24L, 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, 53L, 54L, 55L, 56L, 57L, 
58L, 59L, 60L, 61L, 62L, 63L, 65L, 67L, 68L, 69L, 71L, 72L, 73L, 
74L, 75L, 76L, 77L, 78L, 79L, 80L, 81L, 82L, 83L, 84L, 85L, 86L, 
87L, 89L, 90L, 91L, 92L, 93L, 94L, 95L, 96L, 97L, 1L, 2L, 3L, 
4L, 5L, 6L, 8L, 9L, 10L, 11L), .Label = c("ALTURAS MUNICIPAL AIRPORT     ", 
"ARCATA AIRPORT                ", "AUBURN MUNICIPAL AIRPORT      ", 
"BARSTOW-DAGGETT AIRPORT       ", "BEALE AIR FORCE BASE          ", 
"BICYCLE LAKE FORT IRWIN AAF   ", "BIG BEAR CITY AIRPORT         ", 
"BISHOP AIRPORT                ", "BLUE CANYON - NYACK ARPT      ", 
"BRACKETT FIELD AIRPORT        ", "BROWN FIELD MUNICIPAL ARPT    ", 
"BUCHANAN FIELD AIRPORT        ", "BURBANK-GLENDALE-PASA ARPT    ", 
"CAMARILLO AIRPORT             ", "CATALINA AIRPORT              ", 
"CHICO MUNICIPAL AIRPORT       ", "CHINO AIRPORT                 ", 
"COLUMBIA AIRPORT              ", "CORONA MUNICIPAL AIRPORT      ", 
"DESERT RESORTS RGNL ARPT      ", "FRESNO YOSEMITE INTERNATIONAL ", 
"FULLERTON MUNICIPAL ARPT      ", "GENERAL WILLIAM J. FOX AIRFIEL", 
"GILLESPIE FIELD AIRPORT       ", "GNOSS FIELD AIRPORT           ", 
"HALF MOON BAY AIRPORT         ", "HANFORD MUNICIPAL AIRPORT     ", 
"HAYWARD EXECUTIVE AIRPORT     ", "HOLLISTER MUNICIPAL AIRPORT   ", 
"IMPERIAL COUNTY AIRPORT       ", "J. WAYNE APT-ORANGE CO APT    ", 
"JACK MCNAMARA FIELD ARPT      ", "LAKE TAHOE AIRPORT            ", 
"LIVERMORE MUNICIPAL ARPT      ", "LOMPOC AIRPORT                ", 
"LONG BEACH / DAUGHERTY FIELD /", "LONNIE POOL FIELD WEAVERVILLE ", 
"LOS ALAMITOS AAF AIRPORT      ", "LOS ANGELES INTERNATIONAL AIRP", 
"MADERA MUNICIPAL AIRPORT      ", "MAMMOTH YOSEMITE AIRPORT      ", 
"MCCLELLAN-PALOMAR AIRPORT     ", "MDSTO CTY-CO H SHAM FD APT    ", 
"MEADOWS FIELD AIRPORT         ", "METRO OAKLAND INTL AIRPORT    ", 
"MOFFETT FEDERAL AIRFLD APT    ", "MOJAVE AIRPORT                ", 
"MONTEREY PENINSULA AIRPORT    ", "MONTGOMERY FIELD AIRPORT      ", 
"MRCD MUNI/MACREADY FLD APT    ", "N Y. MINETA SN JO INTL APT    ", 
"NALF/F. SHERMAN FLD ARPT      ", "NAPA COUNTY AIRPORT           ", 
"NEEDLES AIRPORT               ", "NEVADA COUNTY AIR PARK        ", 
"NRTHORP FLD/HATHRN MUNI AP    ", "NUT TREE AIRPORT              ", 
"OCEANSIDE MUNICIPAL ARPT      ", "ONTARIO INTERNATIONAL ARPT    ", 
"OROVILLE MUNICIPAL AIRPORT    ", "OXNARD AIRPORT                ", 
"PALM SPRINGS INTL AIRPORT     ", "PASO ROBLES MUNICIPAL ARPT    ", 
"PETALUMA MUNICIPAL AIRPORT    ", "PLACERVILLE AIRPORT           ", 
"PORTERVILLE MUNICIPAL ARPT    ", "RED BLUFF MUNICIPAL ARPT      ", 
"REDDING MUNICIPAL AIRPORT     ", "RIVERSIDE MUNICIPAL ARPT      ", 
"ROHNERVILLE AIRPORT           ", "SACRAMENTO EXECUTIVE AIRPORT  ", 
"SACRAMENTO INTL AIRPORT       ", "SACRAMENTO MATHER AIRPORT     ", 
"SALINAS MUNICIPAL AIRPORT     ", "SAN CARLOS AIRPORT            ", 
"SAN DIEGO INTERNATIONAL AIRPOR", "SAN FRANCISCO INTERNATIONAL AI", 
"SAN LUIS CO REGIONAL ARPT     ", "SANTA BARBARA MUNICIPAL AIRPOR", 
"SANTA MARIA PUB/CAPT G A HANCO", "SANTA MONICA MUNI AIRPORT     ", 
"SANTA YNEZ AIRPORT            ", "SISKIYOU COUNTY AIRPORT       ", 
"SOUTH COUNTY AIRPORT OF SANTA ", "SOUTHERN CALIFORNIA LOGISTICS ", 
"STOCKTON METROPOLITAN AIRPORT ", "TEHACHAPI MUNICIPAL AIRPORT   ", 
"TRINITY CENTER AIRPORT        ", "TRUCKEE-TAHOE AIRPORT         ", 
"UKIAH MUNICIPAL AIRPORT       ", "UNIVERSITY AIRPORT            ", 
"VAN NUYS AIRPORT              ", "VISALIA MUNICIPAL AIRPORT     ", 
"WATSONVILLE MUNICIPAL ARPT    ", "WHITMAN AIRPORT               ", 
"YUBA COUNTRY AIRPORT          ", "ZAMPERINI FIELD AIRPORT       "
), class = "factor"), THI_Mean = c(79.3844194129895, 47.9523117470258, 
69.6464461167776, 82.0675696692795, 63.0849350901548, 74.6708232862015, 
100.850537076312, 74.8533760212847, 34.5523583142598, 54.1172284209428, 
61.8235131140842, 49.6201577656199, 56.4969309931864, 53.8597392147438, 
74.4432074783713, 46.837589950195, 64.6122605560279, NaN, 64.2706094582438, 
64.337653411354, 52.2603460785691, 94.02576280203, 45.5601654090819, 
55.0929418268904, 53.9012313226694, 68.3311804947574, 62.2675014979279, 
63.654948977002, 56.0543459752155, 46.7621692338974, 81.9324399493885, 
65.7709029415296, 54.8868962009991, 54.8363909689265, 56.8956616154744, 
45.3120728155095, 54.5572278050216, 61.4196381163456, 92.7612688311182, 
60.6665978683592, 53.7854832395117, 69.5295260343158, 61.3910104784722, 
61.392754698528, 91.558905373712, 59.0707460260094, 51.5927530219057, 
61.4922843624376, 60.0626408263436, 53.497914887709, 65.3054900336712, 
67.0548958858132, 53.2591683242605, 65.8223643726204, 50.8970399973389, 
51.2142000658336, 64.1480393838502, 61.4519885276234, 58.1897392633416, 
71.5578784068541, 67.8204129142394, 71.601978756553, 58.5528424362473, 
54.4067136577575, 56.5068448039998, 64.4002989004288, 59.0557207401386, 
56.0689934073247, 55.1991038843164, 55.9231865358109, 61.5743119963135, 
50.5540085434313, 53.5401150699814, 52.1935345223173, 61.3208716258652, 
46.2035536331972, 80.6273562750492, 52.0374237358233, 87.4361185734358, 
58.1078617762253, 91.5909088460021, 75.4622134300844, 62.8279716343081, 
65.9364128813625, 50.0901362392351, 67.4125318829913, 56.3157177087556, 
31.7479782943601, 60.6632690633163, 56.8037497381875, 67.61491611169, 
51.1968002832636, 62.6089733566731, 74.4704531918518, 59.8603592342609, 
66.0972847423599, 89.0724055109542, 70.6066530222754, 40.3039349206599, 
53.3665353315505), THI_Min = c(52.4557405397089, 33.98, 45.3457093495371, 
25.0225954802818, 51.1785334697297, 29.1511758012436, 28.2510119393172, 
58.2138874079664, 0.502087905669441, 32.7983334230263, 46.6597894959148, 
-11.1629416694279, 42.8124578159009, 27.47094073175, 54.9810099920306, 
-7.16578623928804, 43.2890497467431, Inf, -3.70691599739962, 
37.4694017813562, 16.7264957827268, 27.4482183124485, -13.5975437364969, 
35.6903112057144, 30.2000000000001, 59.4304095606214, 8.7735370709971, 
-2.80405898293145, 35.7697087097596, 33.98, 68.3855026732981, 
25.3133696920698, 28.556343667171, 33.5599938044734, 37.8359070192615, 
30.9228228628589, 46.3999999999999, 40.8603031344418, 64.2502080724629, 
52.1493991780154, 36.1767259310531, 42.10373886285, 53.6749547440094, 
53.5952658714694, 33.7332666403973, 45.3594438239759, 10.7322063083914, 
37.9467060766937, 47.289117456444, 33.0925630861752, 1.90029175361333, 
44.2510827881006, 40.9199096994557, 42.3990582610788, 37.4, -10.139479142008, 
46.6597894959148, 53.6438500522523, 3.61411799940322, 8.37516788708545, 
49.2873234865599, 27.5338335958916, 10.1664311833085, -10.7072039412661, 
42.8465883329053, 52.4497314578047, 45.3457093495371, 17.259303962137, 
46.1362993029647, 37.7879989441233, 55.2183148794405, 10.129529761709, 
46.1362993029647, 10.129529761709, 49.8796278533633, -7.00765625628347, 
60.9804990557964, 30.500203826155, 33.7332666403973, 41.793006926767, 
40.7748936693757, 49.0989499378863, 32.0322286350935, 32, -8.27786646700535, 
47.2702704566806, 19.2826923623029, 1.44285466185288, 44.3337809084761, 
46.3999999999999, 35.3138652463278, 41.7170489863234, 33.7351586623816, 
30.4879219330503, 43.9911273883178, 44.7926129254152, 32.7467168398368, 
58.2191654136387, -12.498112226052, 22.9228778307818), THI_Max = c(109.591786682477, 
60.2333967252995, 82.8768296235874, 107.612764911568, 77.0245053208592, 
102.168364973031, 139.996109979679, 82.6768921516963, 68.6026287228502, 
78.3456284931154, 72.0533015440348, 84.0059030769878, 67.0179354243032, 
70.0490739062396, 82.2054720080854, 68.2291538760102, 79.8358713003518, 
-Inf, 99.2586259600651, 75.7556525031902, 67.0179354243032, 117.52943488297, 
73.5830893311879, 68.65063426021, 69.4335307001204, 77.3076678339767, 
76.9529740661977, 100.379670258497, 62.5970088690744, 64.009438459283, 
98.9476881025318, 78.3456284931154, 66.3151890399475, 60.9878582718125, 
82.2054720080854, 58.3647202290469, 62.1651540978214, 73.5529769693358, 
112.070201509673, 72.6703589319581, 71.8716497050957, 81.3559067801607, 
68.7415770349638, 66.0921490462558, 122.093934473517, 76.1709284501561, 
70.6341779681281, 73.5529769693358, 72.0533015440348, 72.0533015440348, 
92.7299425243837, 84.9072999765804, 61.9348463512754, 84.4328754193385, 
68.6207402936826, 77.8815726214716, 71.8716497050957, 79.7562300236269, 
95.3037028368627, 96.2490043020463, 87.3763071289113, 85.9105487050643, 
76.4751770487016, 86.4975370577019, 73.877504195677, 77.521576933463, 
75.1948807718176, 78.6082511434511, 66.3151890399475, 71.9551337820645, 
67.028237117519, 63.6006960092746, 59.249786941551, 63.8719613651346, 
70.6341779681281, 73.1787331352403, 101.186226933919, 73.5830893311879, 
115.648856045281, 72.6703589319581, 121.651090979094, 99.5352109263185, 
81.5737450432125, 85.3244678886861, 76.5427815713664, 79.1421115384682, 
71.3180616536051, 82.0833455621802, 81.9382809187063, 71.0046150736307, 
89.7955910777776, 60.4042257053666, 73.1787331352403, 94.3704734177277, 
71.4307310204537, 85.4942371284446, 120.376615578723, 79.9743241715952, 
80.7722101628022, 73.5571882695921), THIThreshold_1 = c(68, 68, 
68, 68, 68, 68, 68, 68, 68, 68, 68, 68, 68, 68, 68, 68, 68, 68, 
68, 68, 68, 68, 68, 68, 68, 68, 68, 68, 68, 68, 68, 68, 68, 68, 
68, 68, 68, 68, 68, 68, 68, 68, 68, 68, 68, 68, 68, 68, 68, 68, 
68, 68, 68, 68, 68, 68, 68, 68, 68, 68, 68, 68, 68, 68, 68, 68, 
68, 68, 68, 68, 68, 68, 68, 68, 68, 68, 68, 68, 68, 68, 68, 68, 
68, 68, 68, 68, 68, 68, 68, 68, 68, 68, 68, 68, 68, 68, 68, 68, 
68, 68), THIThreshold_2 = c(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, 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, 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, 72, 72, 72, 72, 72, 
72, 72, 72, 72, 72, 72, 72, 72, 72, 72, 72)), class = c("grouped_df", 
"tbl_df", "tbl", "data.frame"), row.names = c(NA, -100L), groups = structure(list(
    Dates = structure(c(16071, 16072), class = "Date"), .rows = list(
        1:90, 91:100)), row.names = c(NA, -2L), class = c("tbl_df", 
"tbl", "data.frame"), .drop = TRUE))

E eu tenho que aplicar esse código na mesma:

period <- 24
amplitude <- (Dados2$THI_Max - Dados2$THI_Min)/2
x1 <- asin ((Dados2$THIThreshold_1 - Dados2$THI_Mean)/amplitude)
x2 <- pi - x1
x11 <- asin ((Dados2$THI_Mean - Dados2$THIThreshold_1)/amplitude)
x22 <- pi - x11
ad <- pi + asin ((Dados2$THI_Mean - Dados2$THIThreshold_1)/amplitude)
ad2 <- pi + asin((Dados2$THIThreshold_1 - Dados2$THI_Mean)/amplitude)


if (Dados2$THI_Mean >= Dados2$THI_Max) {
  THI_Load <- 0
} else if (Dados2$THIThreshold_1 <= Dados2$THI_Min){
  THI_Load = period * (Dados2$THI_Mean - Dados2$THIThreshold_1)
} else if (Dados2$THIThreshold_1 >= Dados2$THI_Mean){
  THI_Load = (cos(x1) - cos(x2)) * amplitude * period/2/pi - (x2 - x1) * period/2/pi*(Dados2$THIThreshold_1-Dados2$THI_Mean)
} else {
  THI_Load = amplitude * period/pi + (Dados2$THI_Mean - Dados2$THIThreshold_1) * period/2 + (Dados2$THI_Mean - Dados2$THIThreshold_1) * ((ad-pi) * period/pi) - (cos(ad) - cos(pi)) * amplitude * period/pi
}

Mas eu recebo a seguinte mensagem:

Warning messages:
1: In if (Dados2$THI_Mean >= Dados2$THI_Max) { :
  the condition has length > 1 and only the first element will be used
2: In if (Dados2$THIThreshold_1 <= Dados2$THI_Min) { :
  the condition has length > 1 and only the first element will be used
3: In if (Dados2$THIThreshold_1 >= Dados2$THI_Mean) { :
  the condition has length > 1 and only the first element will be used

O que posso fazer para solucionar esse erro?

Outra pergunta, é possível criar uma coluna no data.frame original com o resultado da fórmula acima?

1
  • Correr warnings()Mensagem de aviso: In asin((Dados2$THIThreshold_1 - Dados2$THI_Mean)/amplitude) : NaNs produzidos Commented 25/03/2019 às 19:32

2 Respostas 2

5

O problema é que você está comparando dois vetores, equando a condição if(x > y) pede somente um valor para x e y. Se ao invés de passar somente um valor você passa um vetor de valores, a condição vai pegar somente o primeiro valor e emitir um Warning como você viu.

A maneira mais simples (opinião) é de colocar suas condições em um loop for() onde as condições serão testadas para cada elemento dos dois vetores:

# remover os NAs
Dados2 <- Dados2[!is.na(Dados2$THI_Mean), ]

# criar o vetor `THI_Load` vazio no data frame
Dados2$THI_Load = numeric(nrow(Dados2))

for(i in 1:nrow(Dados2)) {
  if (Dados2$THI_Mean[i] >= Dados2$THI_Max[i]) {
    Dados2$THI_Load[i] <- 0
  } else if (Dados2$THIThreshold_1[i] <= Dados2$THI_Min[i]){
    Dados2$THI_Load[i] = period * (Dados2$THI_Mean[i] - Dados2$THIThreshold_1[i])
  } else if (Dados2$THIThreshold_1[i] >= Dados2$THI_Mean[i]){
    Dados2$THI_Load[i] = (cos(x1[i]) - cos(x2[i])) * amplitude[i] * period/2/pi - (x2[i] - x1[i]) * period/2/pi*(Dados2$THIThreshold_1[i]-Dados2$THI_Mean[i])
  } else {
    Dados2$THI_Load[i] = amplitude[i] * period/pi + (Dados2$THI_Mean[i] - Dados2$THIThreshold_1[i]) * period/2 + (Dados2$THI_Mean[i] - Dados2$THIThreshold_1[i]) * ((ad[i]-pi) * period/pi) - (cos(ad[i]) - cos(pi)) * amplitude[i] * period/pi
  }
}

Se você tiver um data.frame muito grande, dê uma olhada em funções como apply() ou do.call() para ganhar eficiência.

1
3

Uma maneira completamente vetorizada, o que em R é sempre boa ideia, é a seguinte.

Começo com uma linha de código emprestada da resposta do Willian Vieira, para criar a tabela Dados3.

Dados3 <- Dados2 <- Dados2[!is.na(Dados2$THI_Mean), ]

i1 <- Dados3$THI_Mean < Dados3$THI_Max
i2 <- Dados3$THIThreshold_1 <= Dados3$THI_Min
i3 <- Dados3$THIThreshold_1 >= Dados3$THI_Mean

Dados3$THI_Load <- 0
Dados3$THI_Load[i1 & i2] <- period * (Dados2$THI_Mean[i1 & i2] - Dados2$THIThreshold_1[i1 & i2])
Dados3$THI_Load[i1 & !i2 & i3] <- (cos(x1[i1 & !i2 & i3]) - cos(x2[i1 & !i2 & i3])) * amplitude[i1 & !i2 & i3] * period/2/pi - (x2[i1 & !i2 & i3] - x1[i1 & !i2 & i3]) * period/2/pi*(Dados3$THIThreshold_1[i1 & !i2 & i3]-Dados3$THI_Mean[i1 & !i2 & i3])
Dados3$THI_Load[i1 & !i2 & !i3] <- amplitude[i1 & !i2 & !i3] * period/pi + (Dados3$THI_Mean[i1 & !i2 & !i3] - Dados3$THIThreshold_1[i1 & !i2 & !i3]) * period/2 + (Dados3$THI_Mean[i1 & !i2 & !i3] - Dados3$THIThreshold_1[i1 & !i2 & !i3]) * ((ad[i1 & !i2 & !i3]-pi) * period/pi) - (cos(ad[i1 & !i2 & !i3]) - cos(pi)) * amplitude[i1 & !i2 & !i3] * period/pi

Depois de correr o código do Willian na resposta do link acima, ver se os resultados são iguais.

identical(Dados2, Dados3)
#[1] TRUE

Como se pode ver, os resultados são iguais.

1

Você deve fazer log-in para responder a esta pergunta.

Esta não é a resposta que você está procurando? Pesquise outras perguntas com a tag .