R将值移动/移位到lappy中的其他列中

R将值移动/移位到lappy中的其他列中,r,dataframe,data-manipulation,R,Dataframe,Data Manipulation,项目概述: 我正在做一个文档提取项目,使用docxtracr库从多个文件中提取word表,并将它们转换为数据帧。下面的代码用于特定的表,即第6个表 由于有多个文件,我使用lappy迭代每个文件并完成数据帧操作任务。数据框有一个额外列的原因是Word表中有隐藏的值用于计算。这些表过去被绑定到一个大型而复杂的宏excel文件 我的代码 library("docxtractr") sourcesSummary <- lapply(files, function(x){ doc &l

项目概述:

我正在做一个文档提取项目,使用docxtracr库从多个文件中提取word表,并将它们转换为数据帧。下面的代码用于特定的表,即第6个表

由于有多个文件,我使用lappy迭代每个文件并完成数据帧操作任务。数据框有一个额外列的原因是Word表中有隐藏的值用于计算。这些表过去被绑定到一个大型而复杂的宏excel文件

我的代码

  library("docxtractr")
  sourcesSummary <- lapply(files, function(x){
   doc <- read_docx(x)
   kingsTbls <- docx_extract_all_tbls(doc)
   sources <- docx_extract_tbl(doc, 6, header = FALSE)
   sources <- data.frame(sources)
   # The below two lines are the issue #
   sources[9,3:4] <- sources[9,2:3]
   sources[24,3:4] <- sources[24,2:3]
   })
                          V1         V2            V3       V4
1           SOURCES OF FUNDS     AMOUNT      PER UNIT     <NA>
2            Proposed A Loan         87 $7,208,000.00  $82,851
3            Proposed B Loan                    $0.00    $   0
4            Investor Equity            $1,948,362.00  $22,395
5   Operating Partner Equity                    $0.00    $   0
6                      Other                    $0.00    $   0
7                      Other                    $0.00    $   0
8                      Other                    $0.00    $   0
9      TOTAL SOURCE OF FUNDS $9,156,362      $105,246     <NA>
10                                 <NA>          <NA>     <NA>
11             USES OF FUNDS     AMOUNT      PER UNIT     <NA>
12      Existing Mortgage(s)                       $0    $   0
13            Purchase Price               $9,011,000 $103,575
14          Origination Fees                  $54,060    $ 621
15        FM application Fee                   $7,208    $  83
16             Investor Fees                    $0.00    $   0
17             Closing Costs                    $0.00    $   0
18 Other (Yield Maintenance)                       $0    $   0
19      Capital Improvements               $39,650.00    $ 456
20            Processing Fee                $3,000.00    $  34
21               Third Party               $11,000.00    $ 126
22                     Legal               $12,500.00    $ 144
23    Repair Escrow (Funded)                    $0.00    $   0
24        TOTAL USE OF FUNDS $9,138,418      $105,039     <NA>
25                                 <NA>          <NA>     <NA>
26        CASH OUT/(CASH IN)        132       $17,944    $ 207
   sources[9,3:4] <- sources[9,2:3]
   sources[24,3:4] <- sources[24,2:3]
所需输出

请注意,第9行和第24行已更改。当我在控制台中的特定数据帧上运行代码时,这就是生成的结果,同样,lappy似乎是问题所在。之后,我将删除第2列和NA行

                          V1         V2            V3       V4
1           SOURCES OF FUNDS     AMOUNT      PER UNIT     <NA>
2            Proposed A Loan         87 $7,208,000.00  $82,851
3            Proposed B Loan                    $0.00    $   0
4            Investor Equity            $1,948,362.00  $22,395
5   Operating Partner Equity                    $0.00    $   0
6                      Other                    $0.00    $   0
7                      Other                    $0.00    $   0
8                      Other                    $0.00    $   0
9      TOTAL SOURCE OF FUNDS               $9,156,362 $105,246
10                                 <NA>          <NA>     <NA>
11             USES OF FUNDS     AMOUNT      PER UNIT     <NA>
12      Existing Mortgage(s)                       $0    $   0
13            Purchase Price               $9,011,000 $103,575
14          Origination Fees                  $54,060    $ 621
15        FM application Fee                   $7,208    $  83
16             Investor Fees                    $0.00    $   0
17             Closing Costs                    $0.00    $   0
18 Other (Yield Maintenance)                       $0    $   0
19      Capital Improvements               $39,650.00    $ 456
20            Processing Fee                $3,000.00    $  34
21               Third Party               $11,000.00    $ 126
22                     Legal               $12,500.00    $ 144
23    Repair Escrow (Funded)                    $0.00    $   0
24        TOTAL USE OF FUNDS               $9,138,418 $105,039
25                                 <NA>          <NA>     <NA>
26        CASH OUT/(CASH IN)        132       $17,944    $ 207
V1 V2 V3 V4
1资金来源单位金额
2提议贷款87美元7208000.00美元82851美元
3拟议B类贷款0.00美元0.00美元
4投资者权益1948362.00美元22395美元
5运营伙伴权益0.00美元0.00美元
6其他$0.00$0
7其他$0.00$0
8其他$0.00$0
9资金来源总额9156362美元105246美元
10
11每单位资金使用金额
12现有抵押贷款0$0
13购买价格9011000美元103575美元
14发起费54060美元621美元
15 FM申请费7208美元83美元
16投资者费用$0.00$0
17结算成本0.00美元0.00美元
18其他(收益维持)$0$0
19资本改进39650.00美元456美元
20处理费$3000.00$34
21第三方11000.00美元126美元
22法定金额12500.00美元144美元
23维修代管(资金)$0.00$0
24资金使用总额9138418美元105039美元
25
26提款/(提款)132美元17944美元207美元

提前感谢您的输入

由函数计算的最后一个表达式由函数返回。在您的例子中,这就是
来源[24,3:4]
                          V1         V2            V3       V4
1           SOURCES OF FUNDS     AMOUNT      PER UNIT     <NA>
2            Proposed A Loan         87 $7,208,000.00  $82,851
3            Proposed B Loan                    $0.00    $   0
4            Investor Equity            $1,948,362.00  $22,395
5   Operating Partner Equity                    $0.00    $   0
6                      Other                    $0.00    $   0
7                      Other                    $0.00    $   0
8                      Other                    $0.00    $   0
9      TOTAL SOURCE OF FUNDS               $9,156,362 $105,246
10                                 <NA>          <NA>     <NA>
11             USES OF FUNDS     AMOUNT      PER UNIT     <NA>
12      Existing Mortgage(s)                       $0    $   0
13            Purchase Price               $9,011,000 $103,575
14          Origination Fees                  $54,060    $ 621
15        FM application Fee                   $7,208    $  83
16             Investor Fees                    $0.00    $   0
17             Closing Costs                    $0.00    $   0
18 Other (Yield Maintenance)                       $0    $   0
19      Capital Improvements               $39,650.00    $ 456
20            Processing Fee                $3,000.00    $  34
21               Third Party               $11,000.00    $ 126
22                     Legal               $12,500.00    $ 144
23    Repair Escrow (Funded)                    $0.00    $   0
24        TOTAL USE OF FUNDS               $9,138,418 $105,039
25                                 <NA>          <NA>     <NA>
26        CASH OUT/(CASH IN)        132       $17,944    $ 207
           V2       V3
24 $9,138,418 $105,039
library("docxtractr")
sourcesSummary <- lapply(files, function(x){
   doc <- read_docx(x)
   kingsTbls <- docx_extract_all_tbls(doc)
   sources <- docx_extract_tbl(doc, 6, header = FALSE)
   sources <- data.frame(sources)
   # The below two lines are the issue #
   sources[9,3:4] <- sources[9,2:3]
   sources[24,3:4] <- sources[24,2:3]
   sources #<- New code!
   })