Conditional formatting on line R

Asked

Viewed 193 times

1

I wanted to replace the empty fields (Nas) in 'number of turns' with the variable (VAR1 - 3) that is filled. I plan to do it in a larger date.frame. As you suggest? inserir a descrição da imagem aqui

3 answers

3

Using the function na.locfpackage zoo, makes it easier to do what you want.

df$N_VOLTAS <- apply(df, 1, function(x) {
  y <- zoo::na.locf(x)
  y[length(y)]
})

Dice.

df <- data.frame(VAR1 = c(rep(NA, 5), 1, 1, 1, rep(NA, 4), 1),
                 VAR2 = c(rep(NA, 3), 19, 13, rep(NA, 8)),
                 VAR3 = c(rep(NA, 8), 10, 8, 7, 5, NA))
df$N_VOLTAS <- NA
df$N_VOLTAS[1:3] <- c(32, 47, 12)
  • Perfect! Thanks to the help of all of you I got here! Thank you very much.

2

You can do the following:

  • For each row identify which one is filled;
  • Associate to the column N_VOLTAS the value of these columns.

I’d do it this way:

df <- data.frame(var1 = c(rep(NA, 5), 1, 1, 1, rep(NA, 4), 1),
                 var2 = c(rep(NA, 3), 19, 13, rep(NA, 8)),
                 var3 = c(rep(NA, 8), 10, 8, 7, 5, NA),
                 n_voltas = c(32, 47, 12, rep(NA, 10)))
>df
   var1 var2 var3 n_voltas
1    NA   NA   NA       32
2    NA   NA   NA       47
3    NA   NA   NA       12
4    NA   19   NA       NA
5    NA   13   NA       NA
6     1   NA   NA       NA
7     1   NA   NA       NA
8     1   NA   NA       NA
9    NA   NA   10       NA
10   NA   NA    8       NA
11   NA   NA    7       NA
12   NA   NA    5       NA
13    1   NA   NA       NA

for (i in 1:nrow(df)) {

  #identificando qual coluna está preenchida
  var1 = is.na(df[i,1])
  var2 = is.na(df[i,2])
  var3 = is.na(df[i,3])

  if (var1 == FALSE){
  df[i, 4] = df[i, 1]
  }else if (var2 == FALSE){
  df[i, 4] = df[i, 2]
  } else if (var3 == FALSE) {
  df[i, 4] = df[i, 3]
  }else {
    #valor para quando todas estiverem vazias

}
}

   var1 var2 var3 n_voltas
1    NA   NA   NA       32
2    NA   NA   NA       47
3    NA   NA   NA       12
4    NA   19   NA       19
5    NA   13   NA       13
6     1   NA   NA        1
7     1   NA   NA        1
8     1   NA   NA        1
9    NA   NA   10       10
10   NA   NA    8        8
11   NA   NA    7        7
12   NA   NA    5        5
13    1   NA   NA        1

This way considers that only one of the columns will be filled and that if you have more than one, the first one will be used in the column n_voltas.

  • Perfect! Thanks to the help of all of you I got here! Thank you very much.

  • isFALSE(var1) is better than var1 == FALSE. Simpler still will be if(!var1) {. And the same for var2 and var3.

  • Good tip! I believe they have several ways to implement such a strategy. Including with the function apply(), for example. Thanks for the tip!

1

You can do it with the package dplyr.

df <- data.frame(var1 = c(rep(NA, 5), 1, 1, 1, rep(NA, 4), 1),
                 var2 = c(rep(NA, 3), 19, 13, rep(NA, 8)),
                 var3 = c(rep(NA, 8), 10, 8, 7, 5, NA))

library(dplyr)

df2 <- df %>% 
  t %>% as.data.frame() %>% 
  dplyr::summarise_each(funs(max(., na.rm = TRUE))) %>% 
  t %>% as.data.frame()

df <- df %>% 
  dplyr::mutate(n_volta = df2$V1)

df
> df
   var1 var2 var3 n_volta
1    NA   NA   NA      32
2    NA   NA   NA      47
3    NA   NA   NA      12
4    NA   19   NA      19
5    NA   13   NA      13
6     1   NA   NA       1
7     1   NA   NA       1
8     1   NA   NA       1
9    NA   NA   10      10
10   NA   NA    8       8
11   NA   NA    7       7
12   NA   NA    5       5
13    1   NA   NA       1

Or:

df2 <- df %>% 
  rowwise() %>% 
  dplyr::mutate(n_volta2 = max(var1, var2, var3, n_volta, na.rm = T))
> df2
Source: local data frame [13 x 4]
Groups: <by row>

# A tibble: 13 x 4
    var1  var2  var3 n_volta
   <dbl> <dbl> <dbl>   <dbl>
 1    NA    NA    NA      32
 2    NA    NA    NA      47
 3    NA    NA    NA      12
 4    NA    19    NA      19
 5    NA    13    NA      13
 6     1    NA    NA       1
 7     1    NA    NA       1
 8     1    NA    NA       1
 9    NA    NA    10      10
10    NA    NA     8       8
11    NA    NA     7       7
12    NA    NA     5       5
13     1    NA    NA       1
  • Opa bbiasi, all right? Thanks for the help, but from what I saw you understood a little differently the request, but anyway helped a lot because your user example in the near future. Thanks!

  • Okay, I edited the answer.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.