R:按行分割的文本文件中的数字

时间:2014-09-21 23:48:58

标签: r

我在读取.t中的.txt时遇到问题。

数据是这样的:

68 89 103 1

37 8 103  9

78 93 8 12

3 50

我在R中使用了readLine()并提出了一个列表。但是当我将它与原始数据进行比较时,我发现,例如,最后一个" 1"在第一行不是1,它应该连接到第二行,这使得数字为e 137,而不是1和37.我认为这个数据被" &#34 ;.如果我使用readLine(),我手动拆分行。我怎么能正确阅读它? 并且,数字9没有连接到78,因为在第3行的开头,有一个空格。数字12与3连接形成123,因为在3之前没有空格。

感谢。我甚至不知道如何在Google中搜索我的问题。不知道如何表达它。

182 63 68 152 130 134 145 152 98 152 182 88 95 105 130 137 167 152 81 71 84 126 134 152 116 130 91 63 68 84 95 152 105 152 63
 102 152 63 77 112 140 77 119 152 161 167 105 112 145 161 182 152 81 95 84 91 102 108 130 134 91

1 2 1 4 3 6 1 1 5 2 1 5 2 3 4 5 5 1 2 6 1

63 102 119 161 161 172 179 88 91 95 105 112 119 119 137 145 167 172 91 98 108 112 134 137 161 161 179 71 174 95 105 134 134 1
37 140 145 150 150 68 68 130 137 77 95 112 137 161 174 81 84 126 134 161 161 174 68 77 98 102 102 102 112 88 88 91 98 112 134
 134 137 137 140 140 152 152 77 179 112 71 71 74 77 112 116 116 140 140 167 77 95 126 150 88 126 130 130 134 63 74 84 84 88 9
1 95 108 134 137 179 81 88 105 116 123 140 145 152 161 161 179 88 95 112 119 126 126 150 157 179 68 68 84 102 105 119 123 123
 137 161 179 182 140 152 182 182 81 63 88 134 84 134 182


7 11 9 2 9 4 6 7 6 1 13 2 1 10 4 5 11 11 9 12 1 3 1 3 3

基本上,我现在正在做的是: 例如,向量:

ind <- c(7, 11, 9, 2 ,9 ,4 ,6, 7, 6 ,1, 13, 2 ,1 ,10 ,4 ,5 ,11 ,11, 9 ,12, 1, 3 ,1, 3 ,3)

表示应根据向量指定的长度拆分上面的数字块。我知道我可以通过

拆分矢量
split(vector, rep(1:length(ind), ind))

然而,问题是我无法正确读取数字块。

1 个答案:

答案 0 :(得分:0)

根据您描述的条件,即在您使用space读取文件后,如果在行的开头有readLines,那么上一行中的最后一个数字应该与当前行的第一个数字。

使用你的第二个例子(我不理解ind

lines1 <- readLines(n=10)
182 63 68 152 130 134 145 152 98 152 182 88 95 105 130 137 167 152 81 71 84 126 134 152 116 130 91 63 68 84 95 152 105 152 63
 102 152 63 77 112 140 77 119 152 161 167 105 112 145 161 182 152 81 95 84 91 102 108 130 134 91

1 2 1 4 3 6 1 1 5 2 1 5 2 3 4 5 5 1 2 6 1

63 102 119 161 161 172 179 88 91 95 105 112 119 119 137 145 167 172 91 98 108 112 134 137 161 161 179 71 174 95 105 134 134 1
37 140 145 150 150 68 68 130 137 77 95 112 137 161 174 81 84 126 134 161 161 174 68 77 98 102 102 102 112 88 88 91 98 112 134
 134 137 137 140 140 152 152 77 179 112 71 71 74 77 112 116 116 140 140 167 77 95 126 150 88 126 130 130 134 63 74 84 84 88 9
1 95 108 134 137 179 81 88 105 116 123 140 145 152 161 161 179 88 95 112 119 126 126 150 157 179 68 68 84 102 105 119 123 123
 137 161 179 182 140 152 182 182 81 63 88 134 84 134 182

 lines2 <- lines1[lines1!=''] #remove blank lines
 indx <- grep("^ ", lines2) #create a numeric index for lines that start with a space
 indx1 <- indx-1 #index that is one above the previous `indx`
 lines2[indx1] <- paste0(lines2[indx1], gsub("^\\s+", "", lines2[indx])) #paste the lines together using the two indexes 
 lines3 <- lines2[-indx] #remove the lines that belong to the first index

 lines3
 #[1] "182 63 68 152 130 134 145 152 98 152 182 88 95 105 130 137 167 152 81 71 84 126 134 152 116 130 91 63 68 84 95 152 105 152 63102 152 63 77 112 140 77 119 152 161 167 105 112 145 161 182 152 81 95 84 91 102 108 130 134 91"                             
 #[2] "1 2 1 4 3 6 1 1 5 2 1 5 2 3 4 5 5 1 2 6 1"                                                                                                                                                                                                                
 #[3] "63 102 119 161 161 172 179 88 91 95 105 112 119 119 137 145 167 172 91 98 108 112 134 137 161 161 179 71 174 95 105 134 134 1"                                                                                                                            
 #[4] "37 140 145 150 150 68 68 130 137 77 95 112 137 161 174 81 84 126 134 161 161 174 68 77 98 102 102 102 112 88 88 91 98 112 134134 137 137 140 140 152 152 77 179 112 71 71 74 77 112 116 116 140 140 167 77 95 126 150 88 126 130 130 134 63 74 84 84 88 9"
 #[5] "1 95 108 134 137 179 81 88 105 116 123 140 145 152 161 161 179 88 95 112 119 126 126 150 157 179 68 68 84 102 105 119 123 123137 161 179 182 140 152 182 182 81 63 88 134 84 134 182"        
相关问题