简体   繁体   English

R将带有if语句的函数应用于列表中的每个元素

[英]R Apply function with if statements to every elements in list

I have a huge list, below is a sample of trboot6 我的清单很大,下面是trboot6的示例

UPDATE: I do not want to delete the extra "1" or "-1". 更新:我不想删除多余的“ 1”或“ -1”。 Instead I want to change it to zero. 相反,我想将其更改为零。 I am so sorry 我很抱歉

dput()
structure(list(`1` = c(-1, 1, -1, -1, -1, -1, -1, -1, -1, 1, 
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1), `2` = c(-1, 
-1, -1, 1, 1, 1, -1, -1, -1, -1, -1, -1, 1, 1, -1, -1, -1, -1, 
-1, 1, 1, 1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 1, 1, 
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1), `3` = c(1, -1, -1, 
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 1, -1, -1, -1, -1, -1, 
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 
-1, -1, -1, 1, -1, -1, -1, -1, -1, -1, -1, -1, -1), `4` = c(-1, 
-1, 1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 
-1, -1, -1, -1, -1, -1, -1, 1, 1, -1, -1, -1, -1, 1, -1, -1, 
-1, -1, -1, -1, -1, -1, -1, -1, -1, 1), .Names = c("1", "2", "3", "4"))

Putting the below for illustration purpose only 以下内容仅供参考

$ 1  : num [1:39] -1 1 -1 -1 -1 -1 -1 -1 -1 1 ...
$ 2  : num [1:46] -1 -1 -1 1 1 1 -1 -1 -1 -1 ...
$ 3  : num [1:48] 1 -1 -1 -1 -1 -1 -1 -1 -1 -1 ...
$ 4  : num [1:43] -1 -1 1 -1 -1 -1 -1 -1 -1 -1 ...

What I want to do is check if in each list every pair has 1 and -1. 我想做的是检查每个列表中每对是否都有1和-1。 Pairs are represented in brackets in the following: 对在以下括号中表示:

$ 1  : num [1:39] (-1 1) (-1 -1) (-1 -1) (-1 -1) (-1 1) ...
$ 2  : num [1:46] (-1 -1) (-1 1) (1 1) (-1 -1) (-1 -1) ...
$ 3  : num [1:48] (1 -1) (-1 -1) (-1 -1) (-1 -1) (-1 -1) ...
$ 4  : num [1:43] (-1 -1) (1 -1) (-1 -1) (-1 -1) (-1 -1) ...

If the pair does not have 1 and -1 then, I want to change the second same number to zero, that is if the pair is (1 1) , I change the second 1 to zero to get '(1 0)'. 如果该对没有1和-1,那么我想将第二个相同的数字更改为零,也就是说,如果该对是(1 1) ,我将第二个1更改为零以获得'(1 0)'。 If there is 1 again, I change this 1 too. 如果再次有1,我也将其更改为1。 Then if there is a -1, it will pair with the first 1. 然后,如果有-1,它将与前1个配对。

To better code, I used the logic that the sum should always remain between -2 and 2 for the pairs to exist. 为了更好地编写代码,我使用了这样的逻辑,即总和应始终在-2和2之间,以使线对存在。 Pair cant be (1,-1) (-1,1) or (1,-1) (1,-1). 对不能为(1,-1)(-1,1)或(1,-1)(1,-1)。 So if the balance goes <-2 or >2, the latest number has to be deleted. 因此,如果余额<-2或> 2,则必须删除最新数字。

Here is my code for the above logic: 这是上述逻辑的代码:

balboot<-0
fboot<- function(x) {
  ifelse(x==-1,balboot<-balbbot-1,balboot<-balboot+1)
  if(balboot==-2){x<-0 
  balboot=-1} 
  if(balboot==2){x<-0 
  balboot=1}
  return(fboot)
}
rdtp<-lapply(trboot6, FUN=fboot)

After running this, I get the warning: 运行此命令后,我得到警告:

In if (x == 1) { ... :
  the condition has length > 1 and only the first element will be used

Expected Output: 预期产量:

list '1': -1, 1, -1, 0, 0, 0, 0, 0, 0, 1, -1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
list '2': -1, 0, 0, 1, 1, 0, -1, -1,0, 0, 0, 0, 1, 1, -1, -1, 0, 0, 0, 1, 1, 0, -1, -1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, -1, -1, 0, 0, 0, 0, 0, 0, 0, 0, 0

thank you for your help in advance. 提前谢谢你的帮助。

SInce I haven't got 50 points of reputation I can't comment, what if you shift your vector from one element and always check that the sum of the vector and the shifted vector is always equal to 0 ? 因为我没有50个信誉点,所以我无法评论,如果将向量从一个元素移开并始终检查向量和移位向量之和始终等于0怎么办?

You won't use apply, but a while condition, it is not really a good looking code but it should work for each vector inside your list. 您不会使用apply,但是会使用一会儿条件,虽然它并不是一个好看的代码,但它应适用于列表中的每个向量。

I created a vector x which is just as a vector from your list (composed of -1 and 1 only) 我创建了一个向量x,它就像您列表中的向量一样(仅由-1和1组成)

 x <- c(-1,1,-1,1,-1,1,-1,1,-1,1,-1,1,1,1,-1,-1,1,1,-1,1,-1,-1,1,-1,1,-1,1)

x1 <- x[-length(x)]
x2 <- x[-1]

y <- x1 + x2

while (length(which((x1+x2)!=0))>0) {
    x <- x[- which((x1+x2)!=0)[1]]
    x1 <- x[-length(x)]
    x2 <- x[-1]
}

Now if I print x I indeed removed all the elements which didn't form a pair of (-1,1) or (1,-1) with the precedent element. 现在,如果我打印x我的确删除了所有没有与先前元素形成一对(-1,1)或(1,-1)的元素。

x
 [1] -1  1 -1  1 -1  1 -1  1 -1  1 -1  1 -1  1 -1  1 -1  1 -1  1 -1  1

I'm not sure this was what you asked, but I hope it will help you at least a little. 我不确定这是否是您的要求,但希望它至少对您有所帮助。

We can define a function that takes in a vector x , and checks for "pairs". 我们可以定义一个接受向量x ,并检查“ pairs”。 If the pair doesn't sum to 0, it replaces the second item of the pair with 0, and then keeps checking along the vector. 如果对的总和不为0,则将对的第二项替换为0,然后继续沿向量进行检查。 We use i and j as our iterators. 我们使用ij作为迭代器。

good_pairs <- function(x){

    i <- 1
    j <- 2
    keepgoing <- TRUE

    while(keepgoing){
        #grab a pair and sum it
        pair <- x[c(i, j)]
        pair_sum <- sum(pair)

        while(pair_sum != 0){ #continue iterating until sum == 0
            #replace i + 1 element with 0
            x[j] <- 0
            j <- j + 1
            #break if we've run out of elements
            if(is.na(x[j])){
                break
            }else{
            #check the pair
            pair <- x[c(i, j)]
            pair_sum <- sum(pair)
            }

        }
        #increment
        i <- j + 1   
        j <- j + 2
        keepgoing <- (length(x) > i)

    }
    x
}

lapply(trboot6, good_pairs)
[[1]]
 [1] -1  1 -1  0  0  0  0  0  0  1 -1  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0

[[2]]
 [1] -1  0  0  1  1  0 -1 -1  0  0  0  0  1  1 -1 -1  0  0  0  1  1  0 -1 -1  0  0  0  0  0  0  0  0  0  1  1 -1 -1  0  0  0  0  0  0  0
[45]  0  0

[[3]]
 [1]  1 -1 -1  0  0  0  0  0  0  0  0  0  0  1 -1  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  1 -1  0  0  0  0
[45]  0  0  0  0

[[4]]
 [1] -1  0  1 -1  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  1  1 -1 -1  0  0  1 -1  0  0  0  0  0  0  0  0  0  0  1

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

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