从 R 中的数据帧生成具有交替行的文本文件

问题描述 投票:0回答:1

我希望使用 R 生成一个文本文件,其中一行显示“128, 128”,下一行是数据帧的第一行(两个值之间有逗号)。那么第三行又是“128, 128”,第四行是数据帧的第二行。该模式在数据帧的 170 万行中持续存在。

输入数据:

abol_cbol_sel <- setNames(data.frame(replicate(2, runif(20L))), c("abol", "cbol"))

> abol_cbol_sel
         abol       cbol
1  0.29636848 0.50432498
2  0.84140843 0.56122708
3  0.62139473 0.17974565
4  0.58794122 0.68818987
5  0.39900351 0.58141138
6  0.01508985 0.46200249
7  0.96268987 0.37889543
8  0.76299114 0.12223663
9  0.96278081 0.69597334
10 0.89542466 0.37967119
11 0.56731021 0.52199498
12 0.43678648 0.89333071
13 0.31440317 0.48038256
14 0.88831876 0.72405470
15 0.76464243 0.26287925
16 0.08912784 0.45741162
17 0.20423405 0.01003791
18 0.82469262 0.99269107
19 0.27169320 0.34495028
20 0.26738678 0.65987557

预期输出:例如,文本文件的前四行将是:

128, 128
0.29636848, 0.50432498
128, 128
0.84140843, 0.56122708

我尝试了这段代码:

out <- (matrix(nrow=2, ncol=3569756))

for (i in 1: nrow(abol_cbol_sel)) {
  out[1,]paste('128, 128', sep=",")
  out[2,]paste(abol_cbol_sel[i])
}
write_lines(out, file="bol.txt", sep=",") 
r dataframe
1个回答
0
投票

这是使用基本 R 的一种方法。

# Dummy data per your example
abol_cbol_sel <- setNames(data.frame(replicate(2, runif(20L))), c("abol", "cbol"))

> abol_cbol_sel
         abol        cbol
1  0.16075967 0.961929468
2  0.06670117 0.968577329
3  0.68362847 0.305907011
4  0.16850073 0.676103643
5  0.78666990 0.540069206
6  0.29216053 0.231236909
7  0.19106370 0.292454021
8  0.60435012 0.924734022
9  0.71236587 0.093686003
10 0.01240113 0.064305871
11 0.07158475 0.001502928
12 0.11337730 0.305517724
13 0.18248184 0.558679583
14 0.03304706 0.187489353
15 0.82306284 0.034983761
16 0.51861392 0.534777389
17 0.45777113 0.634643482
18 0.97615867 0.673790493
19 0.85063190 0.230844911
20 0.04825878 0.374013588

# Create the dataframe for the repeated values
df2 <- data.frame(abol = 128, cbol = 128)

# Receive the output
output1 <- list()

# Just create a mini matrix for each couplet of lines and add them to the list
for (i in 1:nrow(abol_cbol_sel)) {
  output1[[i]] = matrix(c(df2, abol_cbol_sel[i,]), nrow = 2, byrow =T)
}

# Change the list of matrices into a matrix
del1 <- do.call(rbind, output1)

del1
      [,1]       [,2]       
 [1,] 128        128        
 [2,] 0.1607597  0.9619295  
 [3,] 128        128        
 [4,] 0.06670117 0.9685773  
 [5,] 128        128        
 [6,] 0.6836285  0.305907   
 [7,] 128        128        
 [8,] 0.1685007  0.6761036  
 [9,] 128        128        
[10,] 0.7866699  0.5400692  
[11,] 128        128        
[12,] 0.2921605  0.2312369  
[13,] 128        128        
[14,] 0.1910637  0.292454   
[15,] 128        128        
[16,] 0.6043501  0.924734   
[17,] 128        128        
[18,] 0.7123659  0.093686   
[19,] 128        128        
[20,] 0.01240113 0.06430587 
[21,] 128        128        
[22,] 0.07158475 0.001502928
[23,] 128        128        
[24,] 0.1133773  0.3055177  
[25,] 128        128        
[26,] 0.1824818  0.5586796  
[27,] 128        128        
[28,] 0.03304706 0.1874894  
[29,] 128        128        
[30,] 0.8230628  0.03498376 
[31,] 128        128        
[32,] 0.5186139  0.5347774  
[33,] 128        128        
[34,] 0.4577711  0.6346435  
[35,] 128        128        
[36,] 0.9761587  0.6737905  
[37,] 128        128        
[38,] 0.8506319  0.2308449  
[39,] 128        128        
[40,] 0.04825878 0.3740136  

# Make it into a data frame if you want
del1 <- as.data.frame(del1)
© www.soinside.com 2019 - 2024. All rights reserved.