Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/83.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通过数据帧进行扰动的快速方法_R_Dataframe_Apply - Fatal编程技术网

R通过数据帧进行扰动的快速方法

R通过数据帧进行扰动的快速方法,r,dataframe,apply,R,Dataframe,Apply,我有一个数据框架,我正试图用它做一些场景分析。看起来是这样的: Revenue Item_1 Item_2 Item_3 552 200 220 45 1500 400 300 200 2300 600 400 300 Revenue Item_1 Item_2 Item_3 552 201 22

我有一个数据框架,我正试图用它做一些场景分析。看起来是这样的:

Revenue     Item_1    Item_2    Item_3
 552          200       220       45
 1500         400       300       200
 2300         600       400       300
Revenue     Item_1    Item_2    Item_3
 552          201       220       45
 1500         401       300       200
 2300         601       400       300

 552          200       221       45
 1500         400       301       200
 2300         600       401       300

 552          200       220       46
 1500         400       300       201
 2300         600       400       301
我想生成一些东西,其中1个项目增加或减少一定数量(即1个单位),如下所示:

Revenue     Item_1    Item_2    Item_3
 552          200       220       45
 1500         400       300       200
 2300         600       400       300
Revenue     Item_1    Item_2    Item_3
 552          201       220       45
 1500         401       300       200
 2300         601       400       300

 552          200       221       45
 1500         400       301       200
 2300         600       401       300

 552          200       220       46
 1500         400       300       201
 2300         600       400       301
我目前正在这样做,但我想知道是否有更快的方法:

l1 <- list()
increment_amt <- 1
for(i in c('Item_1','Item_2','Item_3')){
   newDf <- df1
   newDf[,i] <- newDf[,i] + increment_amt
   l1[[i]] <- newDf
}

df2 <- do.call(rbind, l1)

l1带
lappy

do.call(rbind, lapply(names(dat)[2:4], function(x) {dat[,x] <- dat[,x] + 1; dat}))
  Revenue Item_1 Item_2 Item_3
1     552    201    220     45
2    1500    401    300    200
3    2300    601    400    300
4     552    200    221     45
5    1500    400    301    200
6    2300    600    401    300
7     552    200    220     46
8    1500    400    300    201
9    2300    600    400    301

使用
lappy

do.call(rbind, lapply(names(dat)[2:4], function(x) {dat[,x] <- dat[,x] + 1; dat}))
  Revenue Item_1 Item_2 Item_3
1     552    201    220     45
2    1500    401    300    200
3    2300    601    400    300
4     552    200    221     45
5    1500    400    301    200
6    2300    600    401    300
7     552    200    220     46
8    1500    400    300    201
9    2300    600    400    301

我们可以编写一个函数并使用
lappy
来完成此任务
df
是原始数据帧<代码>df_列表
是包含所有最终输出的列表。您可以稍后使用
df2我们可以编写一个函数并使用
lappy
来完成此任务
df
是原始数据帧<代码>df_列表
是包含所有最终输出的列表。以后可以使用
df2
#数据帧
数据帧

df您可以使用库(扰动)在R中使用扰动函数。代码如下:

# using the most important features, we create a model
m1 <- lm(revenue ~  item1 + item2 + item3)
#summary(m1)
#anova(m1)
#install.packages("perturb")
library(perturb)
set.seed(1234)
p1_new <- perturb(m1, pvars=c("item1","item2") , prange = c(1,1),niter=20)
p1_new
summary(p1_new)
#使用最重要的功能,我们创建了一个模型

m1您可以使用库(扰动)在R中使用扰动函数。代码如下:

# using the most important features, we create a model
m1 <- lm(revenue ~  item1 + item2 + item3)
#summary(m1)
#anova(m1)
#install.packages("perturb")
library(perturb)
set.seed(1234)
p1_new <- perturb(m1, pvars=c("item1","item2") , prange = c(1,1),niter=20)
p1_new
summary(p1_new)
#使用最重要的功能,我们创建了一个模型
m1