我有很多列表,我想把所有的东西都粘贴在第一行(我不需要,但也不想去掉它(,这样数据:
# A tibble: 4 x 7
financialVariable Currency `2019` `2020` `2021` `2022` `2023`
<chr> <chr> <chr> <chr> <chr> <chr> <chr>
1 Ventas Cliente NA 98716127 1477902 1970536 2955805 4433708
2 Revenue € 987,161 1,477,902 1,970,536 2,955,805 4,433,708
3 Expenditure € 890,623 1,337,031 1,732,873 2,569,122 3,829,434
4 Profit (Loss) € 96,538 140,871 237,663 386,683 604,274
成为:
financialVariable Currency `2019` `2020` `2021` `2022` `2023` New col
<chr> <chr> <chr> <chr> <chr> <chr> <chr>
1 c(Ventas Cliente|98716127|1477902|1970536|2955805|4433708)
2 Revenue € 987,161 1,477,902 1,970,536 2,955,805 4,433,708
3 Expenditure € 890,623 1,337,031 1,732,873 2,569,122 3,829,434
4 Profit (Loss) € 96,538 140,871 237,663 386,683 604,274
因此,第一行只是被粘贴到一个由|
分隔的新列中。我曾想过使用:group_by(.x, financialVariable) %>% summarise(., paste(., collapse = "|"))
,但这将所有行粘贴到一列中,我只想要第一行。
代码:
myList %>%
map(., ~separate(.x, financialVariable, into = c("financialVariable", "Currency"), "n"))
数据:
myList <- list(company1 = structure(list(financialVariable = c("Ventas Cliente",
"Revenuen200", "Expendituren200", "Profit (Loss)n200"),
`2019` = c("98716127", "987,161", "890,623", "96,538"), `2020` = c("1477902",
"1,477,902", "1,337,031", "140,871"), `2021` = c("1970536",
"1,970,536", "1,732,873", "237,663"), `2022` = c("2955805",
"2,955,805", "2,569,122", "386,683"), `2023` = c("4433708",
"4,433,708", "3,829,434", "604,274")), row.names = c(NA,
-4L), groups = structure(list(financialVariable = c("Expendituren200",
"Profit (Loss)n200", "Revenuen200", "Ventas Cliente"), .rows = structure(list(
3L, 4L, 2L, 1L), ptype = integer(0), class = c("vctrs_list_of",
"vctrs_vctr", "list"))), row.names = c(NA, 4L), class = c("tbl_df",
"tbl", "data.frame"), .drop = TRUE), class = c("grouped_df",
"tbl_df", "tbl", "data.frame")), company11 = structure(list(financialVariable = c("B2C and B2B Sales",
"RevenuenCHF", "ExpenditurenCHF", "Profit (Loss)nCHF"), `2019` = c("2",
"69,000", "187,000", "-118,000"), `2020` = c("2", "170,000",
"556,000", "-386,000"), `2021` = c("2", "506,000", "1,102,000",
"-596,000"), `2022` = c("2", "1,269,000", "2,013,000", "-744,000"
), `2023` = c("2", "3,110,000", "3,761,000", "-651,000")), row.names = c(NA,
-4L), groups = structure(list(financialVariable = c("B2C and B2B Sales",
"ExpenditurenCHF", "Profit (Loss)nCHF", "RevenuenCHF"), .rows = structure(list(
1L, 3L, 4L, 2L), ptype = integer(0), class = c("vctrs_list_of",
"vctrs_vctr", "list"))), row.names = c(NA, 4L), class = c("tbl_df",
"tbl", "data.frame"), .drop = TRUE), class = c("grouped_df",
"tbl_df", "tbl", "data.frame")), company7 = structure(list(financialVariable = c("0",
"Revenuen£", "Expendituren£", "Profit (Loss)n£"), `2019` = c("0",
"0", "0", "0"), `2020` = c("0", "0", "0", "0")), row.names = c(NA,
-4L), groups = structure(list(financialVariable = c("0", "Expendituren£",
"Profit (Loss)n£", "Revenuen£"), .rows = structure(list(1L,
3L, 4L, 2L), ptype = integer(0), class = c("vctrs_list_of",
"vctrs_vctr", "list"))), row.names = c(NA, 4L), class = c("tbl_df",
"tbl", "data.frame"), .drop = TRUE), class = c("grouped_df",
"tbl_df", "tbl", "data.frame")))
这似乎是个坏主意。。。但这里有一个方法。
lapply(myList, function(x) {
new_val = do.call(paste, c(x[1, ], sep = "|"))
x[1, ] = NA
x$NewCol = NA
x$NewCol[1] = new_val
x
})
# $company1
# # A tibble: 4 x 7
# # Groups: financialVariable [4]
# financialVariable `2019` `2020` `2021` `2022` `2023` NewCol
# <chr> <chr> <chr> <chr> <chr> <chr> <chr>
# 1 NA NA NA NA NA NA Ventas Cliente|98716127|1477902|1970536|2~
# 2 "Revenuen€" 987,161 1,477,902 1,970,5~ 2,955,8~ 4,433,7~ NA
# 3 "Expendituren€" 890,623 1,337,031 1,732,8~ 2,569,1~ 3,829,4~ NA
# 4 "Profit (Loss)n€" 96,538 140,871 237,663 386,683 604,274 NA
#
# $company11
# # A tibble: 4 x 7
# # Groups: financialVariable [4]
# financialVariable `2019` `2020` `2021` `2022` `2023` NewCol
# <chr> <chr> <chr> <chr> <chr> <chr> <chr>
# 1 NA NA NA NA NA NA B2C and B2B Sales|2|2|2|2|2
# 2 "RevenuenCHF" 69,000 170,000 506,000 1,269,000 3,110,000 NA
# 3 "ExpenditurenCHF" 187,000 556,000 1,102,000 2,013,000 3,761,000 NA
# 4 "Profit (Loss)nCHF" -118,000 -386,000 -596,000 -744,000 -651,000 NA
#
# $company7
# # A tibble: 4 x 4
# # Groups: financialVariable [4]
# financialVariable `2019` `2020` NewCol
# <chr> <chr> <chr> <chr>
# 1 NA NA NA 0|0|0
# 2 "Revenuen£" 0 0 NA
# 3 "Expendituren£" 0 0 NA
# 4 "Profit (Loss)n£" 0 0 NA
如果你告诉我们你为什么这么做,我们可能会帮助你找到更好的方法来实现你的目标。
这里有一种使用匿名函数的可能性:
myList %>%
map(function(x){
nc <- x %>% ungroup() %>% slice(1) %>% gather(date, val, -1) %>% pull(val) %>% paste0(collapse = "|")
x %>% ungroup %>%
mutate(newcol = if_else(1:n() == 1, nc, NA_character_))
})
这里有一个带有reduce
和map
的选项。我们使用map
、ungroup
在list
上循环以删除组属性,然后使用case_when
创建逻辑索引以仅粘贴(str_c
(第一行。默认情况下,其他元素将由NA
填充
library(dplyr)
library(purrr)
library(stringr)
map(myList, ~
.x %>%
ungroup %>%
mutate(newcol = case_when(row_number() == 1
~ reduce(cur_data(), str_c, sep="|"))))
-输出
#company1
# A tibble: 4 x 7
# financialVariable `2019` `2020` `2021` `2022` `2023` newcol
# <chr> <chr> <chr> <chr> <chr> <chr> <chr>
#1 "Ventas Cliente" 98716127 1477902 1970536 2955805 4433708 Ventas Cliente|98716127|1477902|1970536|2955805|…
#2 "Revenuenx80" 987,161 1,477,902 1,970,536 2,955,805 4,433,7… <NA>
#3 "Expenditurenx80" 890,623 1,337,031 1,732,873 2,569,122 3,829,4… <NA>
#4 "Profit (Loss)nx80" 96,538 140,871 237,663 386,683 604,274 <NA>
#$company11
# A tibble: 4 x 7
# financialVariable `2019` `2020` `2021` `2022` `2023` newcol
# <chr> <chr> <chr> <chr> <chr> <chr> <chr>
#1 "B2C and B2B Sales" 2 2 2 2 2 B2C and B2B Sales|2|2|2|2|2
#2 "RevenuenCHF" 69,000 170,000 506,000 1,269,000 3,110,000 <NA>
#3 "ExpenditurenCHF" 187,000 556,000 1,102,000 2,013,000 3,761,000 <NA>
#4 "Profit (Loss)nCHF" -118,000 -386,000 -596,000 -744,000 -651,000 <NA>
#$company7
# A tibble: 4 x 4
# financialVariable `2019` `2020` newcol
# <chr> <chr> <chr> <chr>
#1 "0" 0 0 0|0|0
#2 "Revenuen£" 0 0 <NA>
#3 "Expendituren£" 0 0 <NA>
#4 "Profit (Loss)n£" 0 0 <NA>