[英]How to find count of positive and negative values in a row of dataframe in r
I have a dataframe in r 我在R中有一个数据框
Loss1. Loss2. Loss3
-456. -2345. -1290
345. -342. 234
I want to calculate how many times positive and negative value occurs in a row. 我想计算正值和负值连续出现多少次。 Expected output is as follows 预期输出如下
Loss1. Loss2. Loss3. Neg_count. Pos_count
-456. -2345. -1290. 3. 0
345. -342. 234. 1 2
I tried with rowsums,but it gives me sum of rows. 我尝试使用rowsums,但是它给了我行的总数。 How can I do it in r? 我如何在R中做到这一点?
The code below should work: 下面的代码应该工作:
dat <- data.frame(Loss1=c(-456,345),Loss2=c(-2345,-342),Loss3=c(-1290,234))
dat$Neg_Count <- rowSums(dat[,c("Loss1","Loss2","Loss3")]<0)
dat$Pos_Count <- rowSums(dat[,c("Loss1","Loss2","Loss3")]>0)
dat
Loss1 Loss2 Loss3 Neg_Count Pos_Count
1 -456 -2345 -1290 3 0
2 345 -342 234 1 2
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.