- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
假设有以下数据:
structure(list(uuid = c("abc", "def", "hij"), Q1r1 = c(0L, 1L,
1L), Q1r2 = c(1L, 1L, 1L), Q1r3 = c(1L, 0L, 1L), Q2r1c1 = c(4L,
3L, 5L), Q2r1c2 = 2:4, Q2r1c3 = c(1L, 5L, 2L), Q2r2c1 = c(3L,
3L, 4L), Q2r2c2 = c(2L, 5L, 4L), Q2r2c3 = c(1L, 4L, 5L), Q3r1 = c(5L,
9L, 7L), Q3r2 = c(10L, 3L, 8L), Q3r3 = c(6L, 8L, 5L)), class = "data.frame", row.names = c(NA,
-3L))
这使:
uuid Q1r1 Q1r2 Q1r3 Q2r1c1 Q2r1c2 Q2r1c3 Q2r2c1 Q2r2c2 Q2r2c3 Q3r1 Q3r2 Q3r3
1 abc 0 1 1 4 2 1 3 2 1 5 10 6
2 def 1 1 0 3 3 5 3 5 4 9 3 8
3 hij 1 1 1 5 4 2 4 4 5 7 8 5
现在假设我想对所有 Q1 和 Q3 列的数据进行 pivot_longer(其中这些列中的 r1、r2 和 r3 表示要创建的行)。
dat %>%
pivot_longer(cols = c(starts_with("Q1"), starts_with("Q3")),
names_sep = "r",
names_to = c('.value', 'brand’))
这使:
# A tibble: 9 x 10
uuid Q2r1c1 Q2r1c2 Q2r1c3 Q2r2c1 Q2r2c2 Q2r2c3 brand Q1 Q3
<chr> <int> <int> <int> <int> <int> <int> <chr> <int> <int>
1 abc 4 2 1 3 2 1 1 0 5
2 abc 4 2 1 3 2 1 2 1 10
3 abc 4 2 1 3 2 1 3 1 6
4 def 3 3 5 3 5 4 1 1 9
5 def 3 3 5 3 5 4 2 1 3
6 def 3 3 5 3 5 4 3 0 8
7 hij 5 4 2 4 4 5 1 1 7
8 hij 5 4 2 4 4 5 2 1 8
9 hij 5 4 2 4 4 5 3 1 5
现在,这是我的问题:是否还有一种方法可以将两个枢轴彼此分开,即首先是 pivot_longer Q1,然后是 pivot_longer Q3?
names_sep
和
names_to
的简单代码不再起作用)。一位同事告诉我,在其他软件中,您可以连接单个 pivot_longers,所以我想知道在 R 中是否也可以这样做。
# A tibble: 9 x 10
uuid Q2r1c1 Q2r1c2 Q2r1c3 Q2r2c1 Q2r2c2 Q2r2c3 brand Q1 Q3
<chr> <int> <int> <int> <int> <int> <int> <chr> <int> <int>
1 abc 4 2 1 3 2 1 1 0 5
2 abc 4 2 1 3 2 1 2 1 10
3 abc 4 2 1 3 2 1 3 1 6
4 def 3 3 5 3 5 4 1 1 9
5 def 3 3 5 3 5 4 2 1 3
6 def 3 3 5 3 5 4 3 0 8
7 hij 5 4 2 4 4 5 1 1 7
8 hij 5 4 2 4 4 5 2 1 8
9 hij 5 4 2 4 4 5 3 1 5
我想以 Q2 和 Q3 为中心的第二个示例的所需输出是:
# A tibble: 9 x 8
uuid Q1r1 Q1r2 Q1r3 brand Q2r1 Q2r2 Q3
<chr> <int> <int> <int> <chr> <int> <int> <int>
1 abc 0 1 1 brand1 4 3 5
2 abc 0 1 1 brand2 2 2 10
3 abc 0 1 1 brand3 1 1 6
4 def 1 1 0 brand1 3 3 9
5 def 1 1 0 brand2 3 5 3
6 def 1 1 0 brand3 5 4 8
7 hij 1 1 1 brand1 5 4 7
8 hij 1 1 1 brand2 4 4 8
9 hij 1 1 1 brand3 2 5 5
最佳答案
好吧,在更好地理解了这个问题之后,我能想到的唯一答案是骇人听闻的。你在评论中提到了一个;这是另一个。这围绕使用灵活的正则表达式来选择列。然后它通过 Reduce()
将数据帧连接在一起(或者,如果您愿意,可以将 if 换成 purrr::reduce()
)。另外,请注意,这是执行从宽到长的多个独立时间(和组合),而不是按顺序执行。
col_starts <- c("Q2", "Q3")
lapply(col_starts, function(x) {
df %>%
pivot_longer(matches(paste0("^", x)),
names_pattern = "(Q\\d.*)[rc](\\d)$",
names_to = c(".value", "brand")) %>%
select(uuid, brand:ncol(.), everything(), -matches(paste0("^", setdiff(col_starts, x), collapse = "|")))
}) %>% Reduce(function(x, y) left_join(x, y, by = intersect(names(x), names(y))), .)
# A tibble: 9 x 8
uuid brand Q2r1 Q2r2 Q1r1 Q1r2 Q1r3 Q3
<chr> <chr> <int> <int> <int> <int> <int> <int>
1 abc 1 4 3 0 1 1 5
2 abc 2 2 2 0 1 1 10
3 abc 3 1 1 0 1 1 6
4 def 1 3 3 1 1 0 9
5 def 2 3 5 1 1 0 3
6 def 3 5 4 1 1 0 8
7 hij 1 5 4 1 1 1 7
8 hij 2 4 4 1 1 1 8
9 hij 3 2 5 1 1 1 5
这是一个仅保留
uuid
的版本,
brand
和派生列(imo 更容易阅读)
lapply(c("Q2", "Q3"), function(x) {
df %>%
pivot_longer(matches(paste0("^", x)),
names_pattern = "(Q\\d.*)[rc](\\d)$",
names_to = c(".value", "brand")) %>%
select(uuid, brand, starts_with(x))
}) %>% Reduce(function(x, y) left_join(x, y, by = c("uuid", "brand")), .)
关于R tidyverse 如何在彼此之后做两个 pivot_longer 而不是一个,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63613081/
我正在处理一个包含 528 列和 2,643,246 行的数据框。其中八个是字符变量,其余是整数。总共有 11.35 GiB 的数据,我的可用 RAM 为 164 GiB。 我现在想在所述数据框上运行
我再次努力使用 pivot_longer 将宽 df 转换为长 df 数据框是针对不同效果大小和样本大小进行功效分析的结果,这就是原始 df 的样子: es_issue_owner es_inde
我有一个关于 tidyr::pivot_longer() 的问题。 假设我从一个假想的数据框开始。想象一下,人们被问到关于两个流派的两个问题。 SQ1_2 将是关于第一种类型的第二个问题。 set.s
这个问题在这里已经有了答案: How to use Pivot_longer to reshape from wide-type data to long-type data with multip
我正在使用 tidyr 中的 pivot_longer 将数据帧从宽转换为长。我希望使用所有列并在列中维护行名。较早的melt 函数在此调用中完美运行 w1 % pivot_longer() Erro
我有以下数据框: library(tidyverse) dat % group_by(Scenario) %>% mutate_at(vars(-group_cols()), .funs =
我有一个如下所示的数据集。 # A tibble: 1 x 4 hhm1q001 hhm2q001 hhm1q002 hhm2q002 1 blue re
我有一些看起来很乱的数据,其中多列有多个逗号分隔值: df % select(-ends_with("dur")) %>% pivot_longer(cols = ends_with("aoi
尝试使用 pivot_longer。我不知道如何使用“names_sep”或“names_pattern”来解决这个问题。 dat % mutate(limit=ifelse(limit==
这个问题在这里已经有了答案: How to use Pivot_longer to reshape from wide-type data to long-type data with multip
这个问题在这里已经有了答案: How to use Pivot_longer to reshape from wide-type data to long-type data with multip
我需要将数据框转换为更长的格式。例如,我的数据框会像 df %tidyr::pivot_longer(c("x1","x2","y1","y2"),
我有一个具有这些名称的数据框: df % select(!pooled) %>% mutate(se=!is.na(se)) # A tibble: 14 x 7 state count
上下文 我想将df(宽格式)更改为df_expected(长格式)但失败了。 我知道我需要使用 pivot_longer,但是我需要转换多个列。 > df # A tibble: 2 × 5
这个问题在这里已经有了答案: How to reshape Panel / Longitudinal survey data from wide to long format using pivot
我想知道为什么我的 pivot_longer()下面的调用返回 value 下的嵌套输出柱子? foo % mutate(x = rnorm(n(), mx, 1), y =
我有一个 df 的形式: df 1 1 02 0 1 2 1 03 1 2 3 1 04 0 3 4
我正在尝试使用 R 中的 pivot_longer() 转换表格。但分隔不是通过任何常见符号(例如“_”或“.”)进行的。而是列名称的结尾方式(“B”或“T”)。我尝试使用正则表达式,但不太成功。 下
我有一个带有一系列成对列的“宽”data.frame。我的目标是使用pivot_longer 取消旋转它。 对于所有列中带有前缀的成对列有很多帮助。但在我的情况下,只有一半的对有前缀,我不知道如何定义
简单的 pivot_longer 问题。鉴于: library(tidyverse) df 10 4 2 exit_date 1
我是一名优秀的程序员,十分优秀!