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
在R中使用dplyr创建任意数量的新列_R_Loops_Dplyr_Metadata_Mutate - Fatal编程技术网

在R中使用dplyr创建任意数量的新列

在R中使用dplyr创建任意数量的新列,r,loops,dplyr,metadata,mutate,R,Loops,Dplyr,Metadata,Mutate,我不确定标题的措辞是否恰当,但情况如下: 我有一个元数据数据集,其中可以有任意数量的行,例如: Control_DF <- cbind.data.frame( Scenario = c("A","B","C") ,Variable = c("V1","V2","V3") ,Weight = c("w1","w2","w3") ) Control\u DF在baseR中,使用lappy、Map和cbind可以执行以下操作: # with Control_DF create a

我不确定标题的措辞是否恰当,但情况如下:

我有一个元数据数据集,其中可以有任意数量的行,例如:

Control_DF <- cbind.data.frame(
  Scenario = c("A","B","C")
  ,Variable = c("V1","V2","V3")
  ,Weight = c("w1","w2","w3")
)

Control\u DF在base
R
中,使用
lappy
Map
cbind
可以执行以下操作:

# with Control_DF create a list with pairs of <varName,wgt>

controlVarList = lapply(Control_DF$Scenario,function(x) 

as.vector(as.matrix(Control_DF[Control_DF$Scenario==x,c("Variable","Weight")] )) 

)

controlVarList
#[[1]]
#[1] "V1" "w1"
#
#[[2]]
#[1] "V2" "w2"
#
#[[3]]
#[1] "V3" "w3"


# A custom function for multiplication of both columns

fn_weightedVars = function(x) {

# x  = c("V1","w1"); hence x[1] = "V1",x[2] = "w2"
# reference these columns in Main_Data and do scaling
wgtedCol = matrix(Main_Data[,x[1]] * Main_Data[,x[2]],ncol=1)

#rename as required
colnames(wgtedCol)= paste0("weighted_",x[1]) 

#return var
wgtedCol


}


#call function on each each list element

scaledList = Map(fn_weightedVars ,controlVarList)

如果您不想硬编码,那么逻辑是什么呢?在我的示例中,我们有V1-V3,但在这个应用程序中,我可能有V1-V10或V1-V76,等等。。dplyr的优点是简洁易读的代码,但我正在努力将其扩展到这个问题。我可以做lappy之类的,但我觉得效率会低一些,而且可读性肯定会低一些,这对我来说是不太理想的。如果我能以某种方式将“加权的”\u V1=V1*w1,…,加权的”\u Vn=Vn*wn“放入字符串中,然后将其“粘贴并执行”到mutate函数中,那就太好了!
New_Data <- Main_Data %>%
  mutate(
    weighted_V1 = V1 * w1
    ,weighted_V2 = V2 * w2
    ,weighted_V3 = V3 * w3
  )
# with Control_DF create a list with pairs of <varName,wgt>

controlVarList = lapply(Control_DF$Scenario,function(x) 

as.vector(as.matrix(Control_DF[Control_DF$Scenario==x,c("Variable","Weight")] )) 

)

controlVarList
#[[1]]
#[1] "V1" "w1"
#
#[[2]]
#[1] "V2" "w2"
#
#[[3]]
#[1] "V3" "w3"


# A custom function for multiplication of both columns

fn_weightedVars = function(x) {

# x  = c("V1","w1"); hence x[1] = "V1",x[2] = "w2"
# reference these columns in Main_Data and do scaling
wgtedCol = matrix(Main_Data[,x[1]] * Main_Data[,x[2]],ncol=1)

#rename as required
colnames(wgtedCol)= paste0("weighted_",x[1]) 

#return var
wgtedCol


}


#call function on each each list element

scaledList = Map(fn_weightedVars ,controlVarList)
scaledDF = do.call(cbind,scaledList)

#combine datasets
New_Data  = data.frame(Main_Data,scaledDF)
New_Data
#  V1 V2 V3  w1  w2  w3 weighted_V1 weighted_V2 weighted_V3
#1  1  2  3 0.1 0.2 0.3         0.1         0.4         0.9
#2  2  3  4 0.5 1.0 0.7         1.0         3.0         2.8
#3  3  4  5 1.0 0.3 0.1         3.0         1.2         0.5
#4  4  5  6 0.8 0.6 0.2         3.2         3.0         1.2