Home > other >  Collect column names from all none-zero rows into a new column
Collect column names from all none-zero rows into a new column

Time:10-08

Using the same data set in fill a column with name and content of 3 other columns

dput(df)
structure(list(trtmt = c(26, 28, 35, 42, 49, 63, 33, 24, 38, 
4, 24, 10, 59, 20, 16, 15, 66, 23, 51, 41, 39, 17, 8, 9, 28, 
2, 13, 32, 43, 58, 4, 49, 63, 27, 27, 34, 6, 8, 57, 43, 23, 10, 
50, 47, 41, 65, 57, 21, 15, 35), R = c(150, 150, 0, 0, 25, 25, 
0, 100, 0, 150, 100, 0, 100, 50, 25, 25, 33, 100, 100, 0, 0, 
50, 0, 0, 150, 50, 25, 0, 0, 50, 150, 25, 25, 150, 150, 0, 0, 
0, 25, 0, 100, 0, 50, 100, 0, 100, 25, 100, 25, 0), V = c(50, 
150, 50, 150, 0, 100, 50, 150, 100, 0, 150, 0, 0, 150, 150, 100, 
30.3, 100, 0, 150, 100, 25, 150, 0, 150, 0, 25, 25, 150, 0, 0, 
0, 100, 100, 100, 50, 50, 150, 0, 150, 100, 0, 0, 0, 150, 25, 
0, 25, 100, 50), B = c(0, 0, 100, 50, 50, 25, 25, 0, 50, 0, 0, 
50, 150, 0, 0, 0, 1.8, 0, 50, 25, 100, 0, 0, 25, 0, 0, 0, 150, 
100, 150, 0, 50, 25, 0, 0, 50, 0, 0, 150, 100, 0, 50, 50, 25, 
25, 25, 150, 0, 0, 100)), row.names = c(NA, -50L), class = c("tbl_df", 
"tbl", "data.frame"))

    head(df)
# A tibble: 6 × 4
  trtmt     R     V     B
  <dbl> <dbl> <dbl> <dbl>
1    26   150    50     0
2    28   150   150     0
3    35     0    50   100
4    42     0   150    50
5    49    25     0    50
6    63    25   100    25

I modified the code here Create new column containing names of other columns based on values ​of those columns, telling R to collect all the column names row wise from R through B, for all rows with values > 0 into a new column called key2.

df %>% 
   rowwise() %>%
  mutate(key2 = paste0(names(.)[c_across(R:B) > 0], collapse = ''))

   trtmt     R     V     B key2    
   <dbl> <dbl> <dbl> <dbl> <chr>   
 1    26   150    50     0 trtmtRB 
 2    28   150   150     0 trtmtRB 
 3    35     0    50   100 RV      
 4    42     0   150    50 RV      
 5    49    25     0    50 trtmtVB 
 6    63    25   100    25 trtmtRVB
 7    33     0    50    25 RV      
 8    24   100   150     0 trtmtRB 
 9    38     0   100    50 RV      
10     4   150     0     0 trtmtB  
# … with 40 more rows
# ℹ Use `print(n = ...)` to see more rows

I don't need trtmt in key2. If I do names(.[R:B]), I get an error.

Error in `mutate()`:
! Problem while computing `key2 = paste0(names(.[R:B])[c_across(R:B) > 0], collapse = "")`.
ℹ The error occurred in row 1.
Caused by error in `.[R:B]`:
! Can't subset columns past the end.
ℹ Locations 25, 24, 23, …, 16, and 15 don't exist.
ℹ There are only 14 columns.
Run `rlang::last_error()` to see where the error occurred.

Thanks for your time.

CodePudding user response:

You need to index twice:

cols = c("R", "V", "B")
df %>% 
  rowwise() %>%
  mutate(key2 = paste0(names(.[cols][c_across(cols) > 0]), collapse = ''))
  • Related