我在r中有一个数据帧
Loss1. Loss2. Loss3 -456. -2345. -1290 345. -342. 234
我想计算连续出现正值和负值的次数.预期产量如下
Loss1. Loss2. Loss3. Neg_count. Pos_count -456. -2345. -1290. 3. 0 345. -342. 234. 1 2
我尝试使用rowums,但它给了我一些行.我怎么能在r?
下面的代码应该有效:
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