R将函数与if语句一起应用于列表中的每个元素 [英] R Apply function with if statements to every elements in list
问题描述
我有一个巨大的列表,下面是 trboot6的样本
I have a huge list, below is a sample of trboot6
更新:我不想删除额外的1或-1。相反,我想将其更改为零。我很抱歉
UPDATE: I do not want to delete the extra "1" or "-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 ...
我想要做的是检查每个列表中每对是否有1和-1。对在下面的括号中表示:
What I want to do is check if in each list every pair has 1 and -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) ...
如果该对没有1和-1那么,我想要将第二个相同的数字更改为零,即如果该对是(1 1)
,我将第二个1更改为零以获得'(1 0)'。如果再次有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)'. If there is 1 again, I change this 1 too. Then if there is a -1, it will pair with the first 1.
为了更好的编码,我使用了总和应始终保持在-2和2之间的逻辑对于存在的对。对不能是(1,-1)( - 1,1)或(1,-1)(1,-1)。因此,如果余额<-2或> 2,则必须删除最新的数字。
To better code, I used the logic that the sum should always remain between -2 and 2 for the pairs to exist. Pair cant be (1,-1) (-1,1) or (1,-1) (1,-1). So if the balance goes <-2 or >2, the latest number has to be deleted.
以下是我的上述逻辑代码:
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
预期输出:
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
提前感谢您的帮助。
推荐答案
我们可以定义一个接收向量 x
的函数,并检查对。如果该对不总和为0,则将该对中的第二项替换为0,然后继续沿向量检查。我们使用 i
和 j
作为我们的迭代器。
We can define a function that takes in a vector x
, and checks for "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. We use i
and j
as our iterators.
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
这篇关于R将函数与if语句一起应用于列表中的每个元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!