需要从r中的列中分离出变量名

需要从r中的列中分离出变量名,r,dataframe,dplyr,tidyverse,tidyr,R,Dataframe,Dplyr,Tidyverse,Tidyr,所以我有一个非常糟糕的数据集,我不允许更改。我想使用列Draw_CashFlow,只将某些值放入自己的列中。此外,如果你愿意,我需要将变量都设置为一列周期宽,以便整理 在下面的数据集中,我们有一个列Draw_CashFlow,它以所讨论的变量开始,后跟一个ID列表,然后为下一个变量重复。某些变量可能有NA条目 structure(list(Draw_CashFlow = c("Principal", "R01", "R02", "R03", "Workout Recovery Principa

所以我有一个非常糟糕的数据集,我不允许更改。我想使用列Draw_CashFlow,只将某些值放入自己的列中。此外,如果你愿意,我需要将变量都设置为一列周期宽,以便整理

在下面的数据集中,我们有一个列Draw_CashFlow,它以所讨论的变量开始,后跟一个ID列表,然后为下一个变量重复。某些变量可能有NA条目

structure(list(Draw_CashFlow = c("Principal", "R01", 
"R02", "R03", "Workout Recovery Principal", 
"Prepaid Principal", "R01", "R02", "R03", 
"Interest", "R01", "R02"), `PERIOD 1` = c(NA, 
834659.51, 85800.18, 27540.31, NA, NA, 366627.74, 0, 0, NA, 317521.73, 
29175.1), `PERIOD 2` = c(NA, 834659.51, 85800.18, 27540.31, NA, 
NA, 306125.98, 0, 0, NA, 302810.49, 28067.8), `PERIOD 3` = c(NA, 
834659.51, 85800.18, 27540.31, NA, NA, 269970.12, 0, 0, NA, 298529.92, 
27901.36), `PERIOD 4` = c(NA, 834659.51, 85800.18, 27540.31, 
NA, NA, 307049.06, 0, 0, NA, 293821.89, 27724.4)), row.names = c(NA, 
-12L), class = c("tbl_df", "tbl", "data.frame"))
现在它是一个有限的变量列表,需要本金、回收本金、预付本金和利息,所以我试着做一个循环,看看它是否存在,然后收集,但这是不正确的

在将变量与Draw_CashFlow分开后,我希望前四行类似,忽略变量缩写

ID  Period   Principal  Wrk_Reco_Principal   Prepaid_Principal    Interest
R01      1   834659.51                  NA           366627.74   317521.73
R02      1    85800.18                  NA                0.00    29175.10
R03      1    27540.31                  NA                0.00          NA
R01      2   834659.51                  NA           306125.98   302810.49 

注:Wrl_Reco_本金为NA,因为此变量的提款现金流中没有ID。请记住,这应该是为了对抗任何数量的ID而构建的,但是Draw\u CashFlow列中的变量名称始终是相同的。

这里有一种方法,它假设以R开头的Draw\u CashFlow值是ID号。您可能需要另一种方法,例如!在变量的%LIST\中提取\u现金流%(如果不起作用)

df %>%
  # create separate columns for ID and Variable
  mutate(ID = if_else(Draw_CashFlow %>% str_starts("R"),
                      Draw_CashFlow, NA_character_),
         Variable = if_else(!Draw_CashFlow %>% str_starts("R"),
                        Draw_CashFlow, NA_character_)) %>%
  fill(Variable) %>%  # Fill down Variable in NA rows from above
  select(-Draw_CashFlow) %>%
  gather(Period, value, -c(ID, Variable)) %>%  # Gather into long form
  drop_na() %>%
  spread(Variable, value, fill = 0) %>% # Spread based on Variable
  mutate(Period = parse_number(Period))


# A tibble: 12 x 5
   ID    Period Interest `Prepaid Principal` Principal
   <chr>  <dbl>    <dbl>               <dbl>     <dbl>
 1 R01        1  317522.             366628.   834660.
 2 R01        2  302810.             306126.   834660.
 3 R01        3  298530.             269970.   834660.
 4 R01        4  293822.             307049.   834660.
 5 R02        1   29175.                  0     85800.
 6 R02        2   28068.                  0     85800.
 7 R02        3   27901.                  0     85800.
 8 R02        4   27724.                  0     85800.
 9 R03        1       0                   0     27540.
10 R03        2       0                   0     27540.
11 R03        3       0                   0     27540.
12 R03        4       0                   0     27540.