Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/14.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_Pattern Matching_Replicate - Fatal编程技术网

R 如何在表的上方复制具有特定字符串模式的表行

R 如何在表的上方复制具有特定字符串模式的表行,r,pattern-matching,replicate,R,Pattern Matching,Replicate,我有一个文件中的表格。表格开始前有一个字符行。文件中的表如下所示 XYZ=1 1403244 24041621 3403126 XYZ=2 1405278 24041621 3402144 我希望有复制所有行的输出。例如,第1行到第3行应该重复,结果表总共应该有6行。输出应该如下所示- XYZ=1 1403244 24041621 3403126 4403244 54041621 6403126 XYZ=2 1405278 24041621 3402144 4405278 54041621 64

我有一个文件中的表格。表格开始前有一个字符行。文件中的表如下所示

XYZ=1
1403244
24041621
3403126
XYZ=2
1405278
24041621
3402144

我希望有复制所有行的输出。例如,第1行到第3行应该重复,结果表总共应该有6行。输出应该如下所示-

XYZ=1
1403244
24041621
3403126
4403244
54041621
6403126
XYZ=2
1405278
24041621
3402144
4405278
54041621
6402144


我是R的新手。如果有人能帮我解决这个问题,那就太好了

这应该可以。这肯定不是最干净的方法,但是从初学者的角度来看,函数是非常基本的
test_table.txt
与您在开始时提供的数据相同。我不创建任何列表,输出如下

> output
         [,1] [,2] [,3] [,4] [,5]
XYZ = 1    1   40    3   24    4
           2   40    4   16   21
           3   40    3   12   16
XYZ = 2    1   40    3   24    4
           2   40    4   16   21
           3   40    3   12   16
XYZ = 3    1   40    5   27    8
           2   40    4   16   21
           3   40    2   14   24
XYZ = 4    1   40    5   27    8
           2   40    4   16   21
           3   40    2   14   24
资料
get_rid <- which(unlist(strsplit(data, split = "=")) == "XYZ")
new_data <- data[-c(get_rid[1], get_rid[2:length(get_rid)]-1)]

output <- matrix(nrow = 1, ncol = 5)
for ( i in 1 : length(new_data) ) {
    temp <- unlist(strsplit(new_data[i], " "))
    if ( temp[1] == 1 && i != 1 ) {
        c <- length(output[,1]) 
        output <- rbind(output, output[(c-output[(c-1),1]):(output[c,1]+1),])

        ##output <- rbind(output, output[(i-output[(i-1),1]):(output[(i),1]+1),])
        output <- rbind(output, as.numeric(temp))
    } else {
        output <- rbind(output, as.numeric(temp))
    }
    if ( i == length(new_data) ) {
        output <- rbind(output, output[max(which(output[,1] == 
           1)):length(output[,1]),])
    }
}
output <- output[2:length(output[,1]),]

xyz_names <- character(length(output[,1]))
c <- 1
for ( i in 1 : length(output[,1]) ) {
    if ( output[i,1] == 1 ) {
    xyz_names[i] <- paste("XYZ =", c, collapse = "")
    c <- c + 1
    } else {
        xyz_names[i] <- ""
    }
}

rownames(output) <- xyz_names
##the values of XYZ = ... is
which(output[,1] == 1)
output