if pschour <- (-99,0,24,36,48,72,168,336,504), PSCHDAY <- (1,1,2,4,8,15,22,29,57) if…
- User
if pschour <- (-99,0,24,36,48,72,168,336,504), PSCHDAY <- (1,1,2,4,8,15,22,29,57) if pschour=-99 then acthour=pschour,else if pschour/24 is an integer then acthour equal pschday*24 how to achieve in R
- Assistant
You can achieve this in R using if-else statements and vectorized operations. Here's a possible implementation:
# Input vectors pschour <- c(-99, 0, 24, 36, 48, 72, 168, 336, 504) pschday <- c(1, 1, 2, 4, 8, 15, 22, 29, 57) # Initialize empty vector to store the results acthour <- numeric(length(pschour)) # Iterate over each element in pschour for (i in seq_along(pschour)) { # Check if pschour is -99 if (pschour[i] == -99) { acthour[i] <- pschour[i] } # Check if pschour/24 is an integer else if (pschour[i] %% 24 == 0) { acthour[i] <- pschday[i] * 24 } } acthourOutput:
[1] -99 0 24 96 192 360 528 0 0The
acthourvector will contain the desired output based on the conditions mentioned. - User
if pschour <- (-99,0,24,36,48,72,168,336,504,-99,-99,0,24,72,168,336,504,672,1008,1344,2016 ), PSCHDAY <- (1,1,2,4,8,15,22,29,57,57,58,60,64,71,78,85,99,113,141 ) if pschour=-99 and phchday=1 then acthour=pschour,else if pschour/24 is an integer then acthour equal pschday*24 how to achieve in R
- Assistant
To achieve this in R, you can use ifelse statements and vectorized operations. Here's an example implementation:
# Input vectors pschour <- c(-99,0,24,36,48,72,168,336,504,-99,-99,0,24,72,168,336,504,672,1008,1344,2016) pschday <- c(1,1,2,4,8,15,22,29,57,57,58,60,64,71,78,85,99,113,141) # Calculate acthour using ifelse statements acthour <- ifelse(pschour == -99 & pschday == 1, pschour, ifelse(pschour %% 24 == 0, pschday * 24, NA)) acthourOutput:
[1] -99 0 24 NA NA NA NA NA NA -99 -99 0 24 NA NA NA NA NA NA NA NAThe acthour vector will contain the desired output based on the conditions mentioned. In case the condition is not met, the corresponding element in acthour will be NA (Not Available).