R 检查多个变量以创建新变量的If语句
我创建了一个函数,其中R查看多个变量,然后以如下方式填充一个新列: -如果任何变量有“1”项,则新列应为“1” -如果所有变量都有NA条目,则新列应具有NA值 这应该很简单,但不知何故它不起作用。 我认为问题出在我检查代码的部分,它们都不是NA值:“if(!((is.NA(variable))|…” 有没有更好的编码方法?请帮忙 注意:在这个函数中还有很多计算,但为了显示函数结构和我的具体问题,我只在函数中留下了这一部分R 检查多个变量以创建新变量的If语句,r,function,if-statement,R,Function,If Statement,我创建了一个函数,其中R查看多个变量,然后以如下方式填充一个新列: -如果任何变量有“1”项,则新列应为“1” -如果所有变量都有NA条目,则新列应具有NA值 这应该很简单,但不知何故它不起作用。 我认为问题出在我检查代码的部分,它们都不是NA值:“if(!((is.NA(variable))|…” 有没有更好的编码方法?请帮忙 注意:在这个函数中还有很多计算,但为了显示函数结构和我的具体问题,我只在函数中留下了这一部分 #if they answered "1" (yes) to reciev
#if they answered "1" (yes) to recieving any specific treatment,
#then say "1" (yes) to a new columns called treated_psych
diag_treated <- function(x){
for (v in 1:length(x)) assign(names(x)[v], x[[v]])
if(!((is.na(CurrTx6.1_Group))|(is.na(CurrTx6.1_Ind))| (is.na(CurrTx6.1_Fam))|
(is.na(CurrTx6.1_Couples))|(is.na(CurrTx7a_CBTAnx))|(is.na(CurrTx7b_CBTDep))|
(is.na(CurrTx7c_CBTInsom)))){
if(CurrTx6.1_Group==1 | CurrTx6.1_Ind==1 | CurrTx6.1_Fam==1 | CurrTx6.1_Couples==1 |
CurrTx7a_CBTAnx==1 | CurrTx7b_CBTDep==1 | CurrTx7c_CBTInsom==1)
{
treated_psych <-1
}
else{treated_psych <- 0}
}else{treated_psych<-NA}
treat <- data.frame(treated_psych)
return(treat)
}
#call function
diagnoses_treated <- adply(dataset, 1, diag_treated)
#如果他们回答“1”(是)接受任何特定治疗,
#然后对一个名为“心理治疗”的新栏目说“1”(是)
diag_treated我根据您对数据的描述生成了此样本数据。如果这不正确,请提供可复制的样本数据
sample_data=data.frame("CurrTx6.1_Group"=c(1,1,0,0,NA),
"CurrTx6.1_Fam"=c(NA,NA,0,0,NA),
"CurrTx7b_CBTDep"=c(1,1,0,1,NA))
sample_data
new_var<-rep("xxx",nrow(sample_data)) #Initialize new column variable
for(i in 1:nrow(sample_data)){
if(all(is.na(sample_data[i,]))){
new_var[i]=NA #If any elements in the row are NA, mark the new variable NA
}
}
not_na_index=which(!is.na(new_var)) #Find places where the new value will be 0 or 1
new_var[not_na_index]=rowSums(sample_data, na.rm = TRUE)[not_na_index] #Sum the rows, since everything that is 0 should stay 0, and a single 1 will make the final variable a 1
new_var<-as.numeric(new_var) #Change to numeric (was initialized as string)
new_var[which(new_var>1)]=1 #Change any number higher than 1 to 1
sample_data$new_column=new_var
sample_data
sample_data=data.frame(“CurrTx6.1_组”=c(1,1,0,0,NA),
“CurrTx6.1_Fam”=c(NA,NA,0,0,NA),
“CurrTx7b_CBTDep”=c(1,1,0,1,NA))
样本数据
new_var我最后做了一个列的子集,两个apply函数,然后是一个for循环,它通过apply函数创建的两个向量来生成我的新变量。虽然不是很优雅,也不是很有效,但它可以工作
#if they answered "1" (yes) to recieving any specific treatment,
#then say "1" (yes) to a new columns called treated_psych
#subset data by just these columns
df_psych<- dat_with_pcl5[c("CurrTx6.1_Group", "CurrTx6.1_Ind", "CurrTx6.1_Fam",
"CurrTx6.1_Couples", "CurrTx7a_CBTAnx", "CurrTx7b_CBTDep", "CurrTx7c_CBTInsom")]
#make one vector if ANY are 1, make another vector if ALL are NA
treated_psych1<- apply(df_psych, 1, function(r) any(r %in% "1"))
treated_psych.na<- apply(df_psych, 1, function(r) all(r %in% NA))
# Loop through both vectors and create new variable
#if true treated_psych1 then 1, if true in treated_psych.na then NA
for(i in 1:length(treated_psych0)){
if (treated_psych1[i]==TRUE){treated_psych[i] <- 1}
if (treated_psych.na[i] ==TRUE){treated_psych[i] <- NA}
}
#如果他们回答“1”(是)接受任何特定治疗,
#然后对一个名为“心理治疗”的新栏目说“1”(是)
#仅通过这些列来子集数据
df|u psychth这段代码很糟糕,但我马上就能看到明显的问题。R中的逻辑OR运算符是|
,即两个管道,而不仅仅是一个。看看你是否可以简化你的逻辑。如果你真的没有其他方法来完成这个任务,我可能猜你的数据设计也关闭了。@TimBiegeleisen谢谢你,我在t中改变了这个代码。这似乎并没有解决问题。我将使用双管进行更多测试,减少变量。@TimBiegeleisen:R中有两个逻辑OR运算符。双管运算符在'if()中很有用函数,但单管操作符是矢量化的,因此可能更多地被有经验的R.@42用户使用-是的,我知道(或多或少,可能更少)。