颠倒若干列的内容顺序(最好在tidyverse中实现)。

huangapple go评论105阅读模式
英文:

Reverse the content order of several columns (ideally in tidyverse)

问题

假设以下数据:

  1. df <- data.frame(a = 1:3, b = c(1, 2, 6), c = c(4, 6, NA), d = c(6, NA, NA))

我想要的结果是:

  1. a b c d
  2. 1 1 6 4 1
  3. 2 2 6 2 NA
  4. 3 3 6 NA NA

我考虑过使用 acrossrev 的组合,但目前的尝试不起作用。

英文:

Assuming the following data:

  1. df &lt;- data.frame(a = 1:3, b = c(1, 2, 6), c = c(4, 6, NA), d = c(6, NA, NA))
  2. a b c d
  3. 1 1 1 4 6
  4. 2 2 2 6 NA
  5. 3 3 6 NA NA

And what I want is:

  1. a b c d
  2. 1 1 6 4 1
  3. 2 2 6 2 NA
  4. 3 3 6 NA NA

I thought about some combination of across and rev, but my current attempts don't work.

答案1

得分: 3

以下是翻译好的代码部分:

  1. pivot_longer(df, -a) %>%
  2. filter(!is.na(value)) %>%
  3. mutate(value=rev(value), .by=a) %>%
  4. pivot_wider(names_from = name, values_from = value)

输出结果:

  1. a b c d
  2. <int> <dbl> <dbl> <dbl>
  3. 1 1 6 4 1
  4. 2 2 6 2 NA
  5. 3 3 6 NA NA
英文:

You can do the following:

  1. pivot_longer(df, -a) %&gt;%
  2. filter(!is.na(value)) %&gt;%
  3. mutate(value=rev(value), .by=a) %&gt;%
  4. pivot_wider(names_from = name, values_from = value)

Output:

  1. a b c d
  2. &lt;int&gt; &lt;dbl&gt; &lt;dbl&gt; &lt;dbl&gt;
  3. 1 1 6 4 1
  4. 2 2 6 2 NA
  5. 3 3 6 NA NA

答案2

得分: 2

A base R solution:

  1. df[-1] <- t(apply(df[-1], 1, \(x) c(rev(x[complete.cases(x)]), x[is.na(x)])))

一个基本的R解决方案:

  1. df[-1] <- t(apply(df[-1], 1, \(x) c(rev(x[complete.cases(x)]), x[is.na(x)])))
英文:

A base R solution:

  1. df[-1] &lt;- t(apply(df[-1], 1, \(x) c(rev(x[complete.cases(x)]), x[is.na(x)])))
  2. a b c d
  3. 1 1 6 4 1
  4. 2 2 6 2 NA
  5. 3 3 6 NA NA

答案3

得分: 1

首先定义一个目标列的向量,然后将非NA值逐行粘贴到同一列中。然后将该列分开成宽格式。

  1. library(tidyverse)
  2. target_cols <- c("b", "c", "d")
  3. df %>%
  4. rowwise() %>%
  5. mutate(concat = paste0(rev(na.omit(c_across(all_of(target_cols)))), collapse = ","), .keep = "unused") %>%
  6. separate_wider_delim(cols = concat, names = target_cols, delim = ",", too_few ="align_start")
  7. # A tibble: 3 × 4
  8. a b c d
  9. <int> <chr> <chr> <chr>
  10. 1 1 6 4 1
  11. 2 2 6 2 NA
  12. 3 3 6 NA NA
英文:

First define a vector of target columns, then paste the non-NA values together into the same column row-wise. Then separate that column to a wide format.

  1. library(tidyverse)
  2. target_cols &lt;- c(&quot;b&quot;, &quot;c&quot;, &quot;d&quot;)
  3. df %&gt;%
  4. rowwise() %&gt;%
  5. mutate(concat = paste0(rev(na.omit(c_across(all_of(target_cols)))), collapse = &quot;,&quot;), .keep = &quot;unused&quot;) %&gt;%
  6. separate_wider_delim(cols = concat, names = target_cols, delim = &quot;,&quot;, too_few =&quot;align_start&quot;)
  7. # A tibble: 3 &#215; 4
  8. a b c d
  9. &lt;int&gt; &lt;chr&gt; &lt;chr&gt; &lt;chr&gt;
  10. 1 1 6 4 1
  11. 2 2 6 2 NA
  12. 3 3 6 NA NA

huangapple
  • 本文由 发表于 2023年6月1日 20:39:16
  • 转载请务必保留本文链接:https://go.coder-hub.com/76381953.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定