r - R在按值排序时将列名连接到新列中
问题描述
我正在尝试连接一个字符串,该字符串通过它们的值来标识列的顺序。
set.seed(100)
df <- tibble(id = 1:5,
col1 = sample(1:50, 5),
col2 = sample(1:50, 5),
col3 = sample(1:50, 5)) %>%
mutate_at(vars(-id), ~if_else(. <= 20, NA_integer_, .))
# A tibble: 5 x 4
id col1 col2 col3
<int> <int> <int> <int>
1 1 NA 44 NA
2 2 38 23 34
3 3 48 22 NA
4 4 25 NA 48
5 5 NA NA 43
res <- df %>%
add_column(order = c('col2',
'col2_col3_co1',
'col2_col1',
'col1_col3',
'col3'))
# A tibble: 5 x 5
id col1 col2 col3 order
<int> <int> <int> <int> <chr>
1 1 NA 44 NA col2
2 2 38 23 34 col2_col3_co1
3 3 48 22 NA col2_col1
4 4 25 NA 48 col1_col3
5 5 NA NA 43 col3
我当前的数据是 df 的形式,而我要添加的列是 res 中的 order 列。字符串中元素的排序由每一列的值决定,也需要跳过NA。我试图确定每个 ID 用于填充每列中的值的顺序,因为这些值是以天为单位的时间。但是,并非所有 ID 在所有列中都有值,因此始终存在缺失值。我通常在 tidyverse 中工作,但任何解决方案或想法都将不胜感激。
解决方案
一个更简单的选择是apply
,循环遍历行(MARGIN = 1
),删除 NA
元素,order
其余的非 NA,使用索引来获取列名和paste
它们一起
df$order <- apply(df[-1], 1, function(x) {x1 <- x[!is.na(x)]
paste(names(x1)[order(x1)], collapse="_")})
df$order
#[1] "col2" "col2_col3_col1" "col2_col1" "col1_col3" "col3"
或使用tidyverse
library(dplyr)
library(tidyr)
library(stringr)
df %>%
pivot_longer(cols = -id, values_drop_na = TRUE) %>%
arrange(id, value) %>%
group_by(id) %>%
summarise(order = str_c(name, collapse="_")) %>%
right_join(df) %>%
select(names(df), order)
# A tibble: 5 x 5
# id col1 col2 col3 order
# <int> <int> <int> <int> <chr>
#1 1 NA 44 NA col2
#2 2 38 23 34 col2_col3_col1
#3 3 48 22 NA col2_col1
#4 4 25 NA 48 col1_col3
#5 5 NA NA 43 col3
或使用pmap
frompurrr
library(purrr)
df %>%
mutate(order = pmap_chr(select(., starts_with('col')), ~
{x <- c(...)
x1 <- x[!is.na(x)]
str_c(names(x1)[order(x1)], collapse="_")}))
推荐阅读
- webots - 有没有办法在 webbots 模拟期间在场景树中添加新节点?
- java - 将 Parse SDK 添加到您的 Android 项目并在 android 版本 3.4.1 或 3.4.2 中使用 parse sdk 项目文件
- signalr - 使用不同的 IP 地址连接 Signalr Hub 和客户端
- f# - 我什么时候应该使用 Async.AwaitIAsyncResult 而不是 Async.AwaitTask 来等待任务?
- c# - 在 uwp 应用程序中重新定义应用程序数据文件夹
- docker - Docker Daemon Socket:权限被拒绝(即使在 usermod 之后)
- angular - Angular ngx-datable 条件道具?
- flutter - 有什么方法可以在 Flutter 中的 onPressed() 事件上切换相机?
- python - OpenPyXl 在更改某些单元格值后重新计算
- dialogflow-es - 如何在对话流中创建超链接作为履行的响应