我正在做一个仿真研究,并且已经建立了带有指定功能输入的数据框。
我可以使用一行数据框作为函数的输入吗?请参见下面的for循环。
alpha<-0.05
beta<-0.1
par<-expand.grid(
d_s=c(0.1,0.2,0.3,0.4,0.5),
m=c(5,10,15,20,25,30),
c=c(4,6,8,10,12,14,16,18,20),
rho=c(0,0.01,0.03,0.05,0.07,0.09, 0.1, 0.15,0.2),
Lambda=c(0.25,0.5,1,2,4)
)
par$n0<-c()
for(k in 1:dim(par)[1]){
par$n0[k]<-get.n0(alpha=alpha,beta = beta,
rho = par[k,"rho"],m=par[k,"m"],c=par[k,"c"],d_s = par[k,"d_s"],Lambda = par[k,"Lambda"])
} #not productive
我附加了get.n0,但这对我的问题并不重要。您可以根据需要查看它。
get.n0<-function(one.sided=T,alpha ,beta, d_s, m, c, rho, Lambda=1/(1-rho)){
z1<-ifelse(one.sided,qnorm(1-alpha),qnorm(1-alpha/2)) #if one sided test z1 =z(1-alpha), if two sided z1=z(1-alpha/2)
#print(z1)
z2<-qnorm(1-beta)
#print(z2)
n1<-m*c
n0 <- 1/((d_s/(z1+z2))^2-((Lambda+(m-1)*Lambda*rho)/n1))
if(n0 < 0){
return(0)
}else{
return(ceiling(n0))
}
}
答案 0 :(得分:1)
我们可以将class
与 HTML
<!--PREFERENCE CHECKBOX-->
<div class = "preferanceCheckbox">
<form class ="formBox">
<div>
<input type="checkbox" class = "cBDrivingShot">
Driving Shots <br>
</div>
<input type="checkbox" class = "cBCyberPunkShot">
Cyberpunk <br>
<input type="checkbox" class = "cBcityRelated">
city related <br>
</form>
</div>
<div class ="photoSectionAlignment pintrestView imgZ displayNone">
<!-- CYBERPUNK SHOTS -->
<img src="cyberPunkOne.jpg" class = "pImgCyberPunk pImgDrivingShot displayNone">
<img src="cyberPunkTwo.jpg" class = "pImgCyberPunk pImgCity displayNone">
<img src="cyberPunkTwo.jpg" class = "pImgCyberPunk pImgtext displayNone">
<!-- DRIVING SHOTS -->
<img src="drivingShotOne" class = "pImgDrivingShot pImgCity displayNone">
<img src="drivingShotTwo" class = "pImgDrivingShot pImgCity displayNone">
<img src="drivingShotThree" class = "pImgDrivingShot pImgCyberPunk displayNone">
<!-- CITY SHOTS -->
<img src="cityShotOne" class = "pImg pImgCity displayNone">
</div>
CSS
img {
width: 200px;
}
.displayNone {
display: none;
}
JAVASCRIPT
//GLOBAL VARIABLES
//....................................................................
var pintrestView = document.querySelector(".pintrestView");
var dnPintrest = pintrestView.classList.contains("displayNone");
// GLOBAL PREFERANCES CODE
//..................................................................
var drivingCheckBox = document.querySelector(".cBDrivingShot");
var cyberPunkCheckBox = document.querySelector(".cBCyberPunkShot");
var cityCheckBox = document.querySelector(".cBcityRelated");
//PREFERANCES CODE
//..................................................................
// pintrest class variables
var pImgDrivingShot = document.querySelectorAll(".pImgDrivingShot");
var pImgCyberPunk = document.querySelectorAll(".pImgCyberPunk");
var pImgCity = document.querySelectorAll(".pImgCity");
//DRIVING SHOT FUNCTIONALITY
drivingCheckBox.addEventListener("click",drivingShotImgFunctionPintrest);
function drivingShotImgFunctionPintrest(){
if (drivingCheckBox.checked === true){
for (var i = 0; i < pImgDrivingShot.length; i++){
if (pImgDrivingShot[i].classList.contains("displayNone")) {
pImgDrivingShot[i].classList.remove("displayNone");
}
}
}else{
if (drivingCheckBox.checked === false) {
for (var i = 0; i < pImgDrivingShot.length; i++){
if (pImgDrivingShot[i].classList.contains("displayNone") === false) {
pImgDrivingShot[i].classList.add("displayNone");
}
}
}
}
}
//CYBERPUNK FUNCTIONALITY
cyberPunkCheckBox.addEventListener("click",cyberPunkImgFunctionPintrest);
function cyberPunkImgFunctionPintrest(){
if (cyberPunkCheckBox.checked === true){
for (var i = 0; i < pImgCyberPunk.length; i++){
if (pImgCyberPunk[i].classList.contains("displayNone")) {
pImgCyberPunk[i].classList.remove("displayNone");
}
}
}else{
if (cyberPunkCheckBox.checked === false) {
for (var i = 0; i < pImgCyberPunk.length; i++){
if (pImgCyberPunk[i].classList.contains("displayNone") === false) {
pImgCyberPunk[i].classList.add("displayNone");
}
}
}
}
}
//CITY FUNCTIONALITY
cityCheckBox.addEventListener("click",cityImgFunctionPintrest);
function cityImgFunctionPintrest(){
if (cityCheckBox.checked === true){
for (var i = 0; i < pImgCity.length; i++){
if (pImgCity[i].classList.contains("displayNone")) {
pImgCity[i].classList.remove("displayNone");
}
}
}else{
if (cityCheckBox.checked === false) {
for (var i = 0; i < pImgCity.length; i++){
if (pImgCity[i].classList.contains("displayNone") === false) {
pImgCity[i].classList.add("displayNone");
}
}
}
}
}
逐行使用,并将相应的输入从列传递给函数。
apply
答案 1 :(得分:1)
可以吗?我的意思是..是的,您可以,实际上您已经做到了。但我建议您使用MARGIN = 1
软件包中的output <- apply(par, 1, function(x) {
get.n0(TRUE, alpha, beta, as.numeric(x["d_s"]), as.numeric(x["m"]),
as.numeric(x["c"]), as.numeric(x["rho"]), 0)
})
head(output, 10)
# [1] 857 215 96 54 35 857 215 96 54 35
来完成此操作。
看看这个:
pmap
purrr
始终返回双精度向量。
get.n0<-function(one.sided = TRUE, alpha, beta, d_s, m, c, rho, Lambda = 1/(1-rho)){
z1 <- ifelse(one.sided,qnorm(1-alpha),qnorm(1-alpha/2)) #if one sided test z1 =z(1-alpha), if two sided z1=z(1-alpha/2)
#print(z1)
z2 <- qnorm(1-beta)
#print(z2)
n1 <- m*c
n0 <- 1/((d_s/(z1+z2))^2-((Lambda+(m-1)*Lambda*rho)/n1))
if(n0 < 0){
return(0)
} else {
return(ceiling(n0))
}
}
alpha <- 0.05
beta <- 0.1
par <- expand.grid(
d_s = c(0.1,0.2,0.3,0.4,0.5),
m = c(5,10,15,20,25,30),
c = c(4,6,8,10,12,14,16,18,20),
rho = c(0,0.01,0.03,0.05,0.07,0.09, 0.1, 0.15,0.2),
Lambda = c(0.25,0.5,1,2,4)
)
par$n0 <- purrr::pmap_dbl(par, get.n0, alpha = alpha, beta = beta)
获取一个数据帧,一次占用一行。由于pmap_dbl
的列等于该函数的参数,因此可以正常工作。
从技术上讲,pmap
一次不占用一行,而是在每次i运行时都占用每一列的第i个元素,但无论如何。