How can I find the last column (for each row) that has a value, some rows have all NA values?
I came across with How to find the last column with a value (for each row) in Rsame problem? , except that I have rows with no values (the whole row of NA).The sample provided in the post described does not have a full row of NAs.
I would like to know how should I modify the following? I don't want to wash out those lines with all NAs because they are useful in later analysis.
df %>%
rowwise %>%
mutate(m = {tmp <- c_across(starts_with ('m'))
tail(na.omit(tmp), 1)}) %>%
ungroup
Thank you so much!
uj5u.com enthusiastic netizens replied:
If all elements in the row are empty, then the general solution is to create a condition to pass back NA
these lines font>
library(dplyr)
df %>%
rowwise %>%
mutate(m = {tmp <- c_across(starts_with ('m'))
if(all(is.na( tmp))) NA_character_ else
tail(na.omit(tmp), 1)}) %>%
ungroup
-output
# A tibble: 4 × 5
id m_1 m_2 m_3 m
<dbl> <chr> <chr> <chr> <chr>
1 1 a e i i
2 2 b <NA> <NA> b
3 3 <NA> <NA> <NA> <NA>
4 4 d h l l
If the OP only wants to return the last non-NA element, we can also add an index[1]
to extract, font>NA
When no elementAuto passback
df %>%
rowwise %>%
mutate(m = {tmp <- c_across(starts_with ('m'))
tail(na.omit(tmp), 1)[1]}) %>%
ungroup
# A tibble: 4 × 5
id m_1 m_2 m_3 m
<dbl> <chr> <chr> <chr> <chr>
1 1 a e i i
2 2 b <NA> <NA> b
3 3 <NA> <NA> <NA> <NA>
4 4 d h l l
Data
df <- structure(list(id= c(1, 2, 3, 4), m_1 = c("a", "b", NA, "d"),
m_2 = c("e", NA, NA, "h"), m_3 = c("i", NA, NA, "l")) span>, row.names = c(NA,
-4L), class = "data.frame")
uj5u.com enthusiastic netizens replied:
Using material from @akrun (thanks a lot) we can do this: font>
'\\b[^,] $'
is a regular operation Format:
\\
...means to escape (ie not match) this R
is special in other languages, only A\
\\b
...The metacharacter \b is an anchor ^
and $
notation.It matches at positions called "word boundaries".This match is zero-length.
[^,]
...represents the character class, here special ^
caret: a character that is not,
.
here of "vertical-align: inherit;"> means one or more ,
$
...indicates end of string or end of line, depending on in multiline mode.
library(dplyr)
library(tidyr)
library(stringr)
df %>%
mutate(across(starts_with("m"), ~case_when(!is.na(.) ~ cur_column()),.names = 'new_{col}')) %>%
unite(New_Col, starts_with('new'), na.rm = TRUE, sep = ', ') %>%
mutate(New_Col = str_extract(New_Col, '\\b[^,] $'))
id m_1 m_2 m_3 New_Col
1 1 a e i m_3
2 2 b <NA> <NA> m_1
3 3 <NA> <NA> <NA> <NA>
4 4 d h l m_3
uj5u.com enthusiastic netizens replied:
library(tidyverse)
df <- data.frame(id = c( 1, 2, 3, 4), m_1 = c("a" , NA, "c", " d"), m_2 = c( "e", NA, "g", "h"), m_3 = c( "i", NA, NA, "l"))
df %>%
rowwise() %>%
mutate(
nms = list(str_subset(names( df), "^m")),
m = c_across(starts_with("m")) %>%
{
ifelse(test = all(is.na(.)),
yes = NA,
no = nms[which(.== tail(na.omit(.), 1))]
)
}
) %>%
select(-nms)
#> # A tibble: 4 × 5
#> # Rowwise:
#> id m_1 m_2 m_3 m
#> <dbl> <chr> <chr> <chr> <chr>
#> 1 1 a e i m_3
#> 2 2 <NA> <NA> <NA> <NA>
#> 3 3 c g <NA> m_2
#> 4 4 d h l m_3
# only the value no the column name
df %>%
rowwise() %>%
mutate(
m = c_across(starts_with("m")) %>%
{
ifelse(test = all(is.na(.)),
yes = NA,
no = tail(na.omit(.), 1 )
)
}
)
#> # A tibble: 4 × 5
#> # Rowwise:
#> id m_1 m_2 m_3 m
#> <dbl> <chr> <chr> <chr> <chr>
#> 1 1 a e i i
#> 2 2 <NA> <NA> <NA> <NA>
#> 3 3 c g <NA> g
#> 4 4 d h l l
Byreprex package(v2.0.1)Created January 1, 2022
0 Comments