Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/elixir/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
基于另一个数据帧中的行创建多个新的数据帧,并在r中使用for循环_R_Loops_Dplyr - Fatal编程技术网

基于另一个数据帧中的行创建多个新的数据帧,并在r中使用for循环

基于另一个数据帧中的行创建多个新的数据帧,并在r中使用for循环,r,loops,dplyr,R,Loops,Dplyr,我有一个如下所示的数据帧: df <- data.frame(ID = c(1,2,3,4,5,6), Type = c("A","A","B","B","C","C"), `2019` = c(1,2,3,4,5,6),`2020` = c(2,3,4,5,6,7), `2021` = c(3,4,5,6,7,8)) ID Type X2019 X2020 X2021 1 1 A 1 2 3 2 2 A 2 3 4 3

我有一个如下所示的数据帧:

df <- data.frame(ID = c(1,2,3,4,5,6), Type = c("A","A","B","B","C","C"), `2019` = c(1,2,3,4,5,6),`2020` = c(2,3,4,5,6,7), `2021` = c(3,4,5,6,7,8))

  ID Type X2019 X2020 X2021
1  1    A     1     2     3
2  2    A     2     3     4
3  3    B     3     4     5
4  4    B     4     5     6
5  5    C     5     6     7
6  6    C     6     7     8
我有一些事情会让代码复杂化: 1.该代码将在未来几年内运行,不会发生任何变化,这意味着明年data.frame df将不再包含2019-2021年,而是2020-2022年。 2.由于data.frame df只是一个最小的可复制示例,因此我需要某种循环。在实际数据中,我有更多的行,因此需要创建更多的数据帧

不幸的是,我不能给你任何代码,因为我完全不知道如何管理它。 在研究过程中,我发现以下代码可能有助于解决年复一年的第一个问题:

year <- as.numeric(format(Sys.Date(), "%Y"))
此外,我阅读了有关列表的内容,它可能有助于在for循环中使用列表,然后将列表转换回数据帧。对不起,我的方法有限,我希望任何人都能给我一个提示,甚至解决我的问题。如果你需要任何进一步的信息,请告诉我。提前谢谢

一个类似于我的问题: 试试这个:

图书馆长 图书馆弹琴 图书馆三年 图书馆杂志 df%>% 年份,值,3:5%>% mutateYear=str_子年,2%>% selectID,年份,值%>% 组分割ID分割。$ID [[1]] 一个tibble:3x3 ID年份值 1 1 2019 1 2 1 2020 2 3 1 2021 3 [[2]] 一个tibble:3x3 ID年份值 1 2 2019 2 2 2 2020 3 3 2 2021 4 [[3]] 一个tibble:3x3 ID年份值 1 3 2019 3 2 3 2020 4 3 3 2021 5 [[4]] 一个tibble:3x3 ID年份值 1 4 2019 4 2 4 2020 5 3 4 2021 6 [[5]] 一个tibble:3x3 ID年份值 1 5 2019 5 2 5 2020 6 3 5 2021 7 [[6]] 一个tibble:3x3 ID年份值 1 6 2019 6 2 6 2020 7 3 6 2021 8 数据
df选择来自dplyr。如果您添加librarydplyr,则此功能有效。select来自dplyr,但dplyr也来自tidyverse。。。因此@deepseefan的建议应该很有效。不幸的是,我收到以下错误:group_split中的错误,ID:找不到函数group_split我仔细检查了这两个包,如果它们被激活。@经济专家,如果group_split困扰您,请将其替换为split。$ID,它应该可以工作。在更新我的R和dplyr包后,我可以确认,你的两个变种都工作得很好。谢谢你的时间。非常感谢你的回答。你对年度名称中的X非常关注。不幸的是,我收到以下错误:组分割中出错,ID:找不到函数组分割。我已经仔细检查了这两个包,如果它们真的被激活了……如果它不在你的dplyr版本中,你可能没有最新的。如果没有3.6.1,您可能需要更新软件包,甚至是R。
year <- as.numeric(format(Sys.Date(), "%Y"))
library(magrittr)
library(tidyr)
library(dplyr)
library(stringr)

names(df) <- str_replace_all(names(df), "X", "") #remove X's from year names

df %>%
  gather(Year, Values, 3:5) %>%
  select(ID, Year, Values) %>%
  group_split(ID)