简体   繁体   English

for的for循环中的if / else语句-希望if语句包含多个可能的值

[英]if/else statement in for loop for R - want if statement to include multiple possible values

I'm using R and I'm trying to write a for loop to analyze my data. 我正在使用R,并且试图编写一个for循环来分析我的数据。 The problem I'm having is that some of my replicates were sampled 6 times and some were sampled 4 times (I was growing cultures and some died after 4 time points and others lived throughout the experiment and were sampled 6 times). 我遇到的问题是,我的一些重复样本被采样了6次,而某些样本被采样了4次(我正在培养培养物,有些在4个时间点后死亡,而另一些在整个实验中都存活了下来,并被采样了6次)。 I just want to set up a conditional if/else loop within my for loop to cut off NA values (for points at which that culture wasn't sampled). 我只想在我的for循环中设置一个条件if / else循环以切断NA值(针对未采样该文化的点)。 Here's what I have: 这是我所拥有的:

names <- colnames(culture_data)
for(i in 1:72){
    n <- as.factor(names[i])
    chl.sub <- subset(culture_data, select = n)
    if (n == c("X1", "X2", "X3", "X10", "X11", "X12", "X37", "X38", "X39", "X46", "X47", "X48"))
        chl.sub <- as.matrix(chl.sub[1:4,])
        data <- (data.frame(time_newAgNP, chl.sub))
        }
    else {
        data <- (data.frame(time, chl.sub))
        }

So I want the subsetted data for cultures 1-3, 10-12, 37-39, and 46-48 to be cut after row 4 and the subsetted data for all the other cultures to be used completely. 因此,我希望在第4行之后删除文化1-3、10-12、37-39和46-48的子集数据,并完全使用所有其他文化的子集数据。

I think my main problem is in the first "if" statement in that I don't know how to word it to tell R to do this conditional if n equals any of those values. 我认为我的主要问题是在第一个“ if”语句中,因为如果n等于这些值中的任何一个,我不知道该如何用语告诉R这样做。

Let me know if something's unclear. 让我知道是否有不清楚的地方。 Thank you! 谢谢!

Assuming your data looks like this 假设您的数据如下所示

       t1         t2          t3         t4         t5
1  1.00867689 -1.0286160 -0.13429176 -2.3891856  1.2285634
2 -0.06094606 -0.5265711 -0.52767898         NA         NA
3  1.60603566  0.8295580 -0.44729021 -0.1297540 -1.5007802
4  0.13809702  0.5940972  0.80628674         NA         NA
5  0.45239500  0.6797742 -0.03644485  0.7555041  0.4816549

then 然后

missing = subset(test,is.na(rowSums(test)))
nonmissing = subset(test,!is.na(rowSums(test)))

will work. 将工作。

Although, as Aaron said, make sure that discarding this data is wise before you get rid of it. 尽管,正如亚伦所说,请确保在删除数据之前将其丢弃是明智的。 a categorical variable identifying the early death populations might work. 识别早期死亡人群的分类变量可能会起作用。 There are probably better ways to deal with it though. 不过,也许有更好的方法来处理它。

Cheers, Davy 欢呼声,戴维

If you are trying to create 2 data sets, one data set with just columns with no missing observations and one data set with just columns containing missing observations maybe something like this will work: 如果您尝试创建2个数据集,那么一个仅包含没有缺失观测值的列的数据集和一个仅包含包含缺失观测值的列的数据集可能会起作用:

a <- matrix(c(1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,NA,NA,19,20,NA,NA,21,22), nrow=6, byrow=T)
a

zz <- which( !(is.na( colSums(a))) )

# columns without missing observations
b<- a[,zz]
b

# columns with missing observations
c<- a[1:4,-zz]
c

Here is a loop that selects rows in a column 这是一个选择列中的行的循环

for(i in 1:ncol(a)) {    if(is.na(sum(a[,i]) )) b = a[1:4,i] else b= a[1:6,i] ; print(b) }

Neither might work if there are missing observations in rows 1 through 4. 如果第1到4行中缺少观察值,则两者都不起作用。

May be just na.omit? 可能只是na.omit? It will remove incomplete cases from your dataset. 它将从数据集中删除不完整的案例。

data <- na.omit(culture_data)

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

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