- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
% pivot_longer(cols = ends_with("aoi-6ren">
我有一些看起来很乱的数据,其中多列有多个逗号分隔值:
df <- data.frame(
Line = 1:2,
Utterance = c("hi there", "how're ya"),
A_aoi = c("C*B*C", "*"),
A_aoi_dur = c("100,25,30,50,144", "200"),
B_aoi = c("*A", "*A*A*C"),
B_aoi_dur = c("777,876", "50,22,33,100,150,999")
)
我想做的是 pivot_longer
,这样每个逗号分隔值都有自己的行。我可以完成这个,但看起来我完成的方式是什么,因为它涉及多个中间步骤和临时 df
使代码冗长和繁重:
library(dplyr)
library(tidyr)
# first temporary `df`:
df1 <- df %>%
select(-ends_with("dur")) %>%
pivot_longer(cols = ends_with("aoi"),
names_to = "Speaker") %>%
separate_rows(value, sep = "(?!^|$)") %>%
mutate(Speaker = sub("^(.).*", "\\1", Speaker)) %>%
rename(AOI = value)
# second temporary `df`:
df2 <- df %>%
select(-ends_with("aoi")) %>%
pivot_longer(cols = ends_with("dur")) %>%
separate_rows(value, sep = ",") %>%
rename(Dur = value)
# final `df` (aka, the **expected outcome**):
df3 <- cbind(df1, df2[,4])
df3
Line Utterance Speaker AOI Dur
1 1 hi there A C 100
2 1 hi there A * 25
3 1 hi there A B 30
4 1 hi there A * 50
5 1 hi there A C 144
6 1 hi there B * 777
7 1 hi there B A 876
8 2 how're ya A * 200
9 2 how're ya B * 50
10 2 how're ya B A 22
11 2 how're ya B * 33
12 2 how're ya B A 100
13 2 how're ya B * 150
14 2 how're ya B C 999
如何更简洁地实现这种转变?
最佳答案
这里是tidyverse
中的一个选项
粘贴
'_AOI'重命名(rename_with
)ends_with
'_aoi'pivot_longer
,
以形成通用分隔符 - str_replace_all
separate_rows
在 ,
分隔符上library(dplyr)
library(tidyr)
library(stringr)
df %>%
rename_with(~ str_c(., "_AOI"), ends_with("_aoi")) %>%
pivot_longer(cols = contains("_"),
names_to = c("Speaker", ".value"), names_pattern = "^(.*)_([^_]+$)") %>%
mutate(AOI = str_replace_all(AOI, "(?<=.)(?=.)", ",")) %>%
separate_rows(c(AOI, dur), sep = ",", convert = TRUE)
-输出
# A tibble: 14 x 5
Line Utterance Speaker AOI dur
<int> <chr> <chr> <chr> <int>
1 1 hi there A_aoi C 100
2 1 hi there A_aoi * 25
3 1 hi there A_aoi B 30
4 1 hi there A_aoi * 50
5 1 hi there A_aoi C 144
6 1 hi there B_aoi * 777
7 1 hi there B_aoi A 876
8 2 how're ya A_aoi * 200
9 2 how're ya B_aoi * 50
10 2 how're ya B_aoi A 22
11 2 how're ya B_aoi * 33
12 2 how're ya B_aoi A 100
13 2 how're ya B_aoi * 150
14 2 how're ya B_aoi C 999
关于r - 如何在逗号分隔值数量不等的多列上进行 pivot_longer,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68120708/
我正在处理一个包含 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
我是一名优秀的程序员,十分优秀!