Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/74.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R 查找具有特定属性的第一列_R - Fatal编程技术网

R 查找具有特定属性的第一列

R 查找具有特定属性的第一列,r,R,在数据帧中,经过一些计算后,所有行都以一系列0结尾,如以下(部分)示例所示: X1 X2 X3 X4 X5 X6 X7 X8 X9 X10 X11 X12 X13 X14 X15 1 -9 0 0 0 0 0 0 0 0 0 0 0 0 0 0 2 4 -1 1 -1 0 -1 0 0 0 0 0 0 0 0 0 3 3 0 0 0 0 0 0 0 0 0 0 0 0 0

在数据帧中,经过一些计算后,所有行都以一系列0结尾,如以下(部分)示例所示:

   X1 X2 X3 X4 X5 X6 X7 X8 X9 X10 X11 X12 X13 X14 X15
1  -9  0  0  0  0  0  0  0  0   0   0   0   0   0   0
2   4 -1  1 -1  0 -1  0  0  0   0   0   0   0   0   0
3   3  0  0  0  0  0  0  0  0   0   0   0   0   0   0
4   0  0  0  0  0  0  0  0  0   0   0   0   0   0   0
5  -3  0  0  0  0  0  0  0  0   0   0   0   0   0   0
6  -6  0  0  0  0  0  0  0  0   0   0   0   0   0   0
7   4 -4  1 -1  0 -1  0  0  0   0   0   0   0   0   0
8   3 -3  0  0  0  0  0  0  0   0   0   0   0   0   0
9   3  0  0  0  0  0  0  0  0   0   0   0   0   0   0
10  0  0  0  0  0  0  0  0  0   0   0   0   0   0   0
11 -3  0  0  0  0  0  0  0  0   0   0   0   0   0   0
但是:
-一些孤立的0可能在0系列开始之前出现,如第2行和第7行
-有些行完全由0组成,如第4行和第10行
我想创建一个包含以下信息的新列:
“0系列从哪列开始?”
根据上述示例,此新列应包含以下数字:

2, 7, 2, 1, 2, 2, 7, 3, 2, 1, 2
我不知道该怎么做。。。
谢谢你的提示。

这里有一个简单的解决方案。也许还有更复杂的,但它是有效的。假设你的矩阵叫做“x”

# make new colum and fill with zeros
x[,ncol(x)+1] <- 0

#loop through rows and note first instance of zero in new column
for(i in 1:nrow(x)){
  x[i,ncol(x)] <- grep(0, x[i,])[1]
}
#制作新列并用零填充

x[,ncol(x)+1]使用
apply
在每一行上运行
rle
,并获得值等于零且长度大于1的第一个索引(系列开始)

数据

df = structure(list(X1 = c(-9L, 4L, 3L, 0L, -3L, -6L, 4L, 3L, 3L, 
0L, -3L), X2 = c(0L, -1L, 0L, 0L, 0L, 0L, -4L, -3L, 0L, 0L, 0L
), X3 = c(0L, 1L, 0L, 0L, 0L, 0L, 1L, 0L, 0L, 0L, 0L), X4 = c(0L, 
-1L, 0L, 0L, 0L, 0L, -1L, 0L, 0L, 0L, 0L), X5 = c(0L, 0L, 0L, 
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L), X6 = c(0L, -1L, 0L, 0L, 0L, 
0L, -1L, 0L, 0L, 0L, 0L), X7 = c(0L, 0L, 0L, 0L, 0L, 0L, 0L, 
0L, 0L, 0L, 0L), X8 = c(0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 
0L), X9 = c(0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L), X10 = c(0L, 
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L), X11 = c(0L, 0L, 0L, 
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L), X12 = c(0L, 0L, 0L, 0L, 0L, 
0L, 0L, 0L, 0L, 0L, 0L), X13 = c(0L, 0L, 0L, 0L, 0L, 0L, 0L, 
0L, 0L, 0L, 0L), X14 = c(0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 
0L, 0L), X15 = c(0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L)), .Names = c("X1", 
"X2", "X3", "X4", "X5", "X6", "X7", "X8", "X9", "X10", "X11", 
"X12", "X13", "X14", "X15"), class = "data.frame", row.names = c(NA, 
-11L))

运行完美!谢谢。
df = structure(list(X1 = c(-9L, 4L, 3L, 0L, -3L, -6L, 4L, 3L, 3L, 
0L, -3L), X2 = c(0L, -1L, 0L, 0L, 0L, 0L, -4L, -3L, 0L, 0L, 0L
), X3 = c(0L, 1L, 0L, 0L, 0L, 0L, 1L, 0L, 0L, 0L, 0L), X4 = c(0L, 
-1L, 0L, 0L, 0L, 0L, -1L, 0L, 0L, 0L, 0L), X5 = c(0L, 0L, 0L, 
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L), X6 = c(0L, -1L, 0L, 0L, 0L, 
0L, -1L, 0L, 0L, 0L, 0L), X7 = c(0L, 0L, 0L, 0L, 0L, 0L, 0L, 
0L, 0L, 0L, 0L), X8 = c(0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 
0L), X9 = c(0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L), X10 = c(0L, 
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L), X11 = c(0L, 0L, 0L, 
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L), X12 = c(0L, 0L, 0L, 0L, 0L, 
0L, 0L, 0L, 0L, 0L, 0L), X13 = c(0L, 0L, 0L, 0L, 0L, 0L, 0L, 
0L, 0L, 0L, 0L), X14 = c(0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 
0L, 0L), X15 = c(0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L)), .Names = c("X1", 
"X2", "X3", "X4", "X5", "X6", "X7", "X8", "X9", "X10", "X11", 
"X12", "X13", "X14", "X15"), class = "data.frame", row.names = c(NA, 
-11L))