小编aio*_*orr的帖子

R 中的 hub_wider 具有多列,但将以前的变量名称保留在单独的列中

假设我希望通过不同的setosa-ID 将数据放入宽格式。

iris2 <- iris %>%
          head(10) %>%
          mutate(num = 1:10,
          ID = str_c(Species,1:10))

       Sepal.Length Sepal.Width Petal.Length Petal.Width Species num       ID
1           5.1         3.5          1.4         0.2     setosa   1  setosa1
2           4.9         3.0          1.4         0.2     setosa   2  setosa2
3           4.7         3.2          1.3         0.2     setosa   3  setosa3
4           4.6         3.1          1.5         0.2     setosa   4  setosa4
5           5.0         3.6          1.4         0.2     setosa   5  setosa5
6           5.4         3.9          1.7         0.4     setosa   6  setosa6
7           4.6         3.4          1.4         0.3     setosa   7  setosa7
8           5.0         3.4 …
Run Code Online (Sandbox Code Playgroud)

r tidyr

4
推荐指数
1
解决办法
9350
查看次数

在R中以分层方式在指定字符之前插入一个字符

test.dat <- c("abcde", "abcXe", "abcdY", "abcXY", "abYcXY", "abcYX")
test.want <- c("abcde", "abc1Xe", "abcd1Y", "abc1XY", "abYc1XY", "abcY1X")
Run Code Online (Sandbox Code Playgroud)

假设我希望在“X”或“Y”之前添加“1”,并且如果“X”和“Y”都存在,则仅在“X”之前添加“ 1”。

library(tidyverse)
case_when(
  str_detect(test.dat, "X") ~ str_replace(test.dat, "X", "1X"),
  str_detect(test.dat, "Y") ~ str_replace(test.dat, "Y", "1Y"),
  TRUE ~ as.character(test.dat)
)
Run Code Online (Sandbox Code Playgroud)

这是可行的,但是有没有更好的方法以简洁的方式做到这一点?也许是单人str_replace

如果是“X”或“Y”(先到者为准),第二种情况怎么样?

test.dat <- c("abcde", "abcXe", "abcdY", "abcXY", "abYcXY", "abcYX")
test.want <- c("abcde", "abc1Xe", "abcd1Y", "abc1XY", "ab1YcXY", "abc1YX")
Run Code Online (Sandbox Code Playgroud)

stringr 是更好的选择,但我欢迎任何其他方法。谢谢。

regex r stringr

3
推荐指数
1
解决办法
101
查看次数

使用 dplyr 查找配对列的差异

set.seed(3)
library(dplyr)
dat <- tibble(Measure = c("Height","Weight","Width","Length"),
             AD1_1= rpois(4,10),
             AD1_2= rpois(4,9),
             AD2_1= rpois(4,10),
             AD2_2= rpois(4,9),
             AD3_1= rpois(4,10),
             AD3_2= rpois(4,9),
             AD4_1= rpois(4,10),
             AD4_2= rpois(4,9),
             AD5_1= rpois(4,10),
             AD5_2= rpois(4,9),
             AD6_1= rpois(4,10),
             AD6_2= rpois(4,9))
Run Code Online (Sandbox Code Playgroud)

假设我有这样的数据。我希望计算每个 AD 的差异,并与下划线数字配对,即 AD1diff、AD2diff、AD3diff。

而不是写作

dat %>%
mutate(AD1diff = AD1_1 - AD1_2,
       AD2diff = AD2_1 - AD2_2,
...)
Run Code Online (Sandbox Code Playgroud)

写这个的有效方法是什么?

r dplyr

2
推荐指数
1
解决办法
1134
查看次数

根据其元素而不是其名称选择列

假设我想选择所有包含1.

test.dat <- tibble(
  name = c("A","B","C"),
  alert_A = c(1,1,1),
  alert_B = c(1,1,0),
  alert_C = c(1,0,1),
  alert_D = c(1,0,0),
  alert_E = c(0,0,0)
)

> test.dat
# A tibble: 3 x 6
  name  alert_A alert_B alert_C alert_D alert_E
  <chr>   <dbl>   <dbl>   <dbl>   <dbl>   <dbl>
1 A           1       1       1       1       0
2 B           1       1       0       0       0
3 C           1       0       1       0       0
> test.want
# A tibble: 3 x 5
  name  alert_A alert_B alert_C alert_D
  <chr> …
Run Code Online (Sandbox Code Playgroud)

r dplyr

2
推荐指数
1
解决办法
47
查看次数

标签 统计

r ×4

dplyr ×2

regex ×1

stringr ×1

tidyr ×1