我在names_to参数上看到了另外两个类似的问题。但是,我仍然不确定我是否会使用它。
考虑以下df:
df <- data.frame("Metric" = c("a_exp_2001","a_inc_2001","a_inc_2002","a_exp_2002"),
"John" = c(220,230,240,250),
"Abby" = c(440,450,470,480))
head(df)
Metric John Abby
1 a_exp_2001 220 440
2 a_inc_2001 230 450
3 a_inc_2002 240 470
4 a_exp_2002 250 480
我正在寻找一个新的数据框架,该框架仅具有一年(例如2002年)的上述信息,但格式较长。也就是说,我希望以3列“名称”,“支出”(值对应于a_exp_2002
),“收入”(值对应a_inc_2002
)结束。
我不确定如何在names_to
中指定pivot_longer()
参数来获得最终的df。
到目前为止,我只能做到这一点:
df %>%
pivot_longer(cols = -c(Metric), ...)
答案 0 :(得分:0)
如果我正确理解了您的问题,这可能是解决方案:
library(dplyr)
library(tidyr)
df <- data.frame("Metric" = c("a_exp_2001","a_inc_2001","a_inc_2002","a_exp_2002"),
"John" = c(220,230,240,250),
"Abby" = c(440,450,470,480))
df %>%
# get relevant info from Metric in two columns
dplyr::mutate(IN_or_EX = substr(Metric, start = 3, stop = 5),
YEAR = substr(Metric, start = 7, stop = 10)) %>%
# filter for the year you wan
dplyr::filter(YEAR == 2001) %>%
# drop the Metric column as it presents no value anymore
dplyr::select(-Metric) %>%
# make it longer
tidyr::pivot_longer(-c(IN_or_EX, YEAR), names_to = "NAME", values_to = "VALUES") %>%
# make it wider
tidyr::pivot_wider(names_from = IN_or_EX, values_from = VALUES, values_fill = 0)
YEAR NAME exp inc
<chr> <chr> <dbl> <dbl>
1 2001 Abby 440 450
2 2001 John 220 230
答案 1 :(得分:0)
您可能正在寻找:
pivot_longer(df, -1) %>%
separate(Metric, sep = 6, into = c("type", "year")) %>%
pivot_wider(names_from = type, values_from = value) %>%
rename(Expenditure = a_exp_, Income = a_inc_)
#> # A tibble: 4 x 4
#> year name Expenditure Income
#> <chr> <chr> <dbl> <dbl>
#> 1 2001 John 220 230
#> 2 2001 Abby 440 450
#> 3 2002 John 250 240
#> 4 2002 Abby 480 470
答案 2 :(得分:0)
这是工作吗?
library(tidyverse)
data <- tibble::tribble(
~Metric.John.Abby,
"1 a_exp_2001 220 440",
"2 a_inc_2001 230 450",
"3 a_inc_2002 240 470",
"4 a_exp_2002 250 480"
) %>%
tidyr::separate("Metric.John.Abby", into = c('num', 'metric', 'sp', 'john', 'sp2', 'abby'), sep = " ") %>%
select(metric, john, abby)
data %>%
filter(str_detect(metric, "2002")) %>%
pivot_longer(cols = -metric, names_to = "names") %>%
separate(metric, into = c('letter', 'type', 'year')) %>%
select(type, names, value) %>%
pivot_wider(type, id_cols = c('type', 'names')) %>%
relocate(exp, .before = inc)
#> # A tibble: 2 x 3
#> names exp inc
#> <chr> <chr> <chr>
#> 1 john 250 240
#> 2 abby 480 470
由reprex package(v0.3.0)于2020-11-08创建