[英]How to replace every column value with the value of the next column based on a condition
I should first admit that I really found it difficult to come up with a proper title to the complex issue I am facing.我首先应该承认,我真的很难为我所面临的复杂问题找到一个合适的标题。
I have the following data:我有以下数据:
configuration_id TARGET_CLASS UniqueIdentifier BranchCoverage Total_Branches Size Length Generations Statements_Executed CoverageTimeline_T1 CoverageTimeline_T2 CoverageTimeline_T3
ar_statement com.browsersoft.aacs.User NA 67559dfd 1 60 46 108 NA 108 0.8158776539 0.8381375035
ar_statement com.browsersoft.aacs.User efe4cbdc 1 60 44 103 240 1087446 0.7525773196 0.7540513682 0.7661337337
ar_statement com.browsersoft.aacs.User NA aac8afa6 1 60 43 104 NA 177 0.765031271 0.8062749834
ar_statement com.browsersoft.aacs.User 8567c4bd 1 60 45 105 388 NA 0.8680720145 0.9386218251 0.9484536082
ar_statement com.browsersoft.aacs.User 94e45912 1 60 43 101 118 NA 0.8767466262 0.9471901622 0.9690721649
As you can see there are NAs in the UniqueIdentifier
column.如您所见, UniqueIdentifier
列中有 NA。 The NA pushed the values in the same row to the right side; NA 将同一行中的值推到右侧; the correct value is in the right column.正确的值在右栏中。 What I want is to remove the NA and replace it with the next column value like:我想要的是删除 NA 并将其替换为下一列值,例如:
configuration_id TARGET_CLASS UniqueIdentifier BranchCoverage Total_Branches Size Length Generations Statements_Executed CoverageTimeline_T1 CoverageTimeline_T2 CoverageTimeline_T3
ar_statement com.browsersoft.aacs.User 67559dfd 1 60 46 108 108 NA 0.8158776539 0.8381375035
ar_statement com.browsersoft.aacs.User efe4cbdc 1 60 44 103 240 1087446 0.7525773196 0.7540513682 0.7661337337
ar_statement com.browsersoft.aacs.User aac8afa6 1 60 43 104 177 NA 0.765031271 0.8062749834
ar_statement com.browsersoft.aacs.User 8567c4bd 1 60 45 105 388 NA 0.8680720145 0.9386218251 0.9484536082
ar_statement com.browsersoft.aacs.User 94e45912 1 60 43 101 118 NA 0.8767466262 0.9471901622 0.9690721649
To make it more clear, for those rows where UniqueIdentifier
is NA, then replace the value of each column with value in the next column (it's like pushing the values back).为了更清楚,对于那些UniqueIdentifier
为 NA 的行,然后将每一列的值替换为下一列中的值(这就像将值推回)。
I hope my question is clear.我希望我的问题很清楚。
How can I do that?我怎样才能做到这一点?
I think you are looking for我想你正在寻找
data$UniqueIdentifier <- dplyr::coalesce(data$UniqueIdentifier, data$BranchCoverage)
Or using base R:或使用基础 R:
data$UniqueIdentifier <- ifelse(is.na(data$UniqueIdentifier), data$BranchCoverage, data$UniqueIdentifier)
edit : Your first data is a bit hard to understand, i couldn't see if it was only BranchCoverage that was changed, or every other value in the line.编辑:您的第一个数据有点难以理解,我看不出是只更改了 BranchCoverage 还是该行中的所有其他值。 If every value got pushed to the right, maybe you should check the way you are reading your data.如果每个值都被推到右边,也许你应该检查你读取数据的方式。 But i think you can solve it like this:但我认为你可以这样解决它:
for (i in 1:nrow(data2)){
if(is.na(data2$UniqueIdentifier[i])){
data2[i, 3:ncol(data2)] = c(data2[i, 4:ncol(data2)], NA)
}
}
This is kind of an ugly solution, but it should work.这是一种丑陋的解决方案,但它应该可以工作。
If it was only BranchCoverage and you want to replace it all the values for one, you could do data$BranchCoverage <- 1
.如果它只是 BranchCoverage 并且您想将它的所有值替换为一个,您可以执行data$BranchCoverage <- 1
。
Also, thanks to CPak for the comment.另外,感谢 CPak 的评论。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.