4

I have a dataframe where every row should contains mostly "No response" values (-1). I'd like to get the first value of every row that is not -1, preferably using something tidy-friendly.

# A tibble: 3,222 x 10
   tracc1 tracc2 tracc3 tracc4 tracc5 tracc6 tracc7 tracc8 tracc9 tracc10
   <chr>  <chr>  <chr>  <chr>  <chr>  <chr>  <chr>  <chr>  <chr>  <chr>  
 1 1      -1     -1     -1     -1     -1     7      -1     -1     -1     
 2 1      -1     -1     -1     -1     -1     -1     -1     -1     -1     
 3 1      -1     -1     -1     -1     -1     -1     -1     -1     -1     
 4 1      -1     -1     -1     -1     -1     -1     -1     -1     -1     
 5 1      -1     -1     -1     -1     -1     -1     -1     -1     -1     
 6 1      -1     -1     -1     -1     -1     -1     -1     -1     -1     
 7 1      -1     -1     -1     -1     -1     -1     -1     -1     -1     
 8 1      -1     -1     -1     -1     -1     -1     -1     -1     -1     
 9 -1     -1     3      -1     -1     -1     -1     -1     -1     -1     
10 1      -1     -1     -1     -1     -1     -1     -1     -1     -1     
# ...

I was able to use dpylr::unite to combine all the columns, but problems arise when a single row has multiple valid responses. In the example below, row one should yield 1 rather than 17.

> df %>%
    mutate_at(vars(starts_with("tracc")),
              function(t) {if_else(t < 0,"",t)}) %>%
    unite("tracc",starts_with("tracc"),sep = "", na.rm = TRUE)
# A tibble: 3,222 x 1
   tracc
   <chr>
 1 17
 2 1
 3 1
 4 1
 5 1
 6 1
 7 1
 8 1
 9 3
10 1
# ...
4
  • 1
    What do you want to do when there is no -1 in a row? Oct 28, 2019 at 22:19
  • NA and -1 would be the same in this case.
    – jane
    Oct 28, 2019 at 22:34
  • 2
    Sorry, too early in the day - I meant when there is nothing but -1 in a row. NA presumably? Oct 28, 2019 at 22:36
  • Oh-- yup. That would be fine. I've filtered out every row that doesn't have a non negative number though.
    – jane
    Oct 30, 2019 at 16:06

4 Answers 4

8

Try this simple code:

apply(df, 1, function(x) x[x != -1][1])

It parallelly applies to each row.

1
  • 1
    The most straightforward answer for sure.
    – jane
    Oct 30, 2019 at 16:13
6

One dplyr option could be:

df %>%
 mutate_all(~ replace(., . == "-1", NA_integer_)) %>%
 transmute(tracc = coalesce(!!!.))

   tracc
1      1
2      1
3      1
4      1
5      1
6      1
7      1
8      1
9      3
10     1

An option since dplyr 1.0.0 could be:

df %>%
 transmute(tracc = Reduce(coalesce, across(everything(), ~ replace(., . == "-1", NA_integer_))))
1
  • Take a look at this page, it's explained in-depth: tidyeval.tidyverse.org/multiple.html In short, it means that the function is looking for the first non-NA value in the whole dataset.
    – tmfmnk
    Oct 30, 2019 at 17:07
2

Another way to do it with dplyr

library(dplyr)
df %>% mutate(row_num = row_number()) %>% # add column with row number
       pivot_longer(-row_num,names_to='tracc') %>% # pivot to get three columns
       mutate(tracc=as.numeric(str_replace(tracc,'tracc',''))) %>% # convert tracc to numeric
       filter(value != -1) %>% # keep only -1 values
       arrange(tracc) %>% # sort by tracc
       group_by(row_num)  %>% 
       filter(row_number()==1) # keep first -1 value by row_num
# A tibble: 10 x 3
# Groups:   row_num [10]
#   row_num tracc value
#     <int> <dbl> <int>
# 1       1     1     1
# 2       2     1     1
# 3       3     1     1
# 4       4     1     1
# 5       5     1     1
# 6       6     1     1
# 7       7     1     1
# 8       8     1     1
# 9      10     1     1
#10       9     3     3
2

We can use vectorized option with row/column indexing

df[cbind(seq_len(nrow(df)), max.col(df != -1, 'first'))]

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.