繁体   English   中英

如何进行循环以将另一个 data.frame 的每两列插入另一个 data.frame 中的每一列?

[英]How to make a loop to insert every column in a data.frame in another by every two columns of the another data.frame?

dfData <- data.frame(DistA = c(10, 8, 15, 22, 15), 
                     DistB = c(15, 35, 40, 33, 20),
                     DistC = c(20,40,50,45,30),
                     DistD = c(60,55,55,48,50))

>dfData
DistA DistB DistC DistD
1    10    15    20    60
2     8    35    40    55
3    15    40    50    55
4    22    33    45    48
5    15    20    30    50

## CREATE THE COLUMNS TO INSERT IN THE dfData
cols <- ceiling(seq_along(dfData[])/2)
new_cols <- tapply(names(dfData[]), cols, function(x) 
  sprintf('diff_%s', paste0(x, collapse = '')))
new_columns<- sapply(split.default(dfData[], cols), function(x) 
 paste(  round((x[[2]] / x[[1]] -1)*100,2)   ,"%",sep=""))
new_columns=as.data.frame(new_columns)


预期输出:

    DistA DistB DiffB-A  DistC DistD  Diff D-C
 1    10    15   50%    20    60    200%
 2     8    35  337.5%  40    55    37.5%
 3    15    40 166.67%  50    55     10%
 4    22    33   50%    45    48    6.67%
 5    15    20  33.33%  30    50   66.67%

试图


sapply(seq(3,length(dfData),by=3),function(x)
    dfData[,x]<-new_columns)

我正在尝试创建一个循环,通过每两列 dfData 在 DfData 中插入 new_column 的列

select from dplyr函数提供了一个解决方案:

library(dplyr)

dfData <- data.frame(DistA = c(10, 8, 15, 22, 15), 
                     DistB = c(15, 35, 40, 33, 20),
                     DistC = c(20,40,50,45,30),
                     DistD = c(60,55,55,48,50))

dfData <- dfData %>%
  mutate(`DistB-A` = paste(round(100*(DistB - DistA)/DistA, 2), "%"),
         `DistD-C` = paste(round(100*(DistD - DistC)/DistC, 2), "%")) %>%
  select(DistA, DistB, `DistB-A`, DistC, DistD, `DistD-C`)

> dfData
  DistA DistB  DistB-A DistC DistD DistD-C
1    10    15     50 %    20    60   200 %
2     8    35  337.5 %    40    55  37.5 %
3    15    40 166.67 %    50    55    10 %
4    22    33     50 %    45    48  6.67 %
5    15    20  33.33 %    30    50 66.67 %

使用select辅助函数,请参见此处,您可以使案例通用。

我发现解决方案只是找到一种方法来创建数字序列以根据列数重新排序列:

dfData <- data.frame(DistA = c(10, 8, 15, 22, 15), 
                     DistB = c(15, 35, 40, 33, 20),
                     DistC = c(20,40,50,45,30),
                     DistD = c(60,55,55,48,50))


## CREATE THE COLUMNS TO INSERT IN THE dfData
cols <- ceiling(seq_along(dfData[])/2)
new_cols <- tapply(names(dfData[]), cols, function(x) 
  sprintf('diff_%s', paste0(x, collapse = '')))
new_columns<- sapply(split.default(dfData[], cols), function(x) 
  paste(  round((x[[2]] / x[[1]] -1)*100,2)   ,"%",sep=""))
new_columns=as.data.frame(new_columns)

 
 # create the sequence of numbers based on the number of columns 1,2,5,3,4,6                    
order=unlist(Map(c, split(1:length(dfData), as.integer(gl(length(dfData), 2, 
                                      length(dfData)))), length(dfData)+seq(1:length(new_columns))), use.names = FALSE)

# join the two dataframes
dfData2=cbind(dfData,new_columns)

#reorder the columns
dfData2=dfData2[,order]
dfData2

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM