[英]Is there a way to move each Nth row of a text file into a specific column in R?
Here is what my text file data looks like:这是我的文本文件数据的样子:
1 Abigail Seltzer 1 阿比盖尔苏打水
6019 8th St NW 6019 第八街西北
Washington, DC-20011华盛顿特区-20011
Not Employed Not Employed未就业 未就业
6019 8th St. NW 6019 第八街西北
Washington,DC-20011华盛顿特区-20011
Credit Card 03/11/2022 $10,000.00 $10,00.00信用卡 03/11/2022 $10,000.00 $10,00.00
2 Danita Cole 2 丹妮塔·科尔
416 Shepherd St NW 416 Shepherd St NW
Washington, DC-20005华盛顿特区-20005
Not Employed Not Employed未就业 未就业
416 Shepherd Street NW西北牧羊人街 416 号
Washington,DC-20011华盛顿特区-20011
Credit Card 03/11/2022 $100.00 $350.00信用卡 03/11/2022 $100.00 $350.00
3 Landon James 3 兰登詹姆斯
612 Quintana Pl NW 612 Quintana PL NW
Washington, DC-20011华盛顿特区-20011
Social Inclusion Manager Brookfield Properties社会包容经理 Brookfield Properties
655 New York Ave NW 655纽约大道NW
Washington,DC-20000华盛顿特区-20000
Credit Card 03/11/2022 $100.00 $100.00信用卡 03/11/2022 $100.00 $100.00
To be clear, each time there is a number before the name (1 Abigail, 2 Danita) the next 6 rows follow without blank rows, then there is one blank row after the 7th row before the next numbered entry (between 'Credit Card 03/11/2022 $10,000.00 $10,00.00' and '2 Danita')).需要明确的是,每次姓名前有一个数字(1 Abigail,2 Danita),接下来的 6 行没有空白行,然后在第 7 行之后的下一个编号条目之前有一个空白行(介于“Credit Card 03”之间/11/2022 $10,000.00 $10,00.00' 和 '2 Danita'))。 So there are no blank rows between '1 Abigail Seltzer' and '6019 8th St NW'.
因此,“1 Abigail Seltzer”和“6019 8th St NW”之间没有空白行。
I would like to put each of the grouped rows into their own 7 columns - for each grouping of 7 grows, so:我想将每一组分组的行放入它们自己的 7 列中——对于每组 7 的增长,所以:
column1 1 Abigail Seltzer 2 Danita Cole 3 Landon James column1 1 Abigail Seltzer 2 Danita Cole 3 Landon James
column2 6019 8th St NW 416 Shepherd St NW 612 Quintana Pl NW column2 6019 8th St NW 416 Shepherd St NW 612 Quintana Pl NW
column 3 Washington, DC-20011 Washington, DC-20005 Washington, DC-20000第 3 列 Washington, DC-20011 Washington, DC-20005 Washington, DC-20000
And so on.等等。
Thanks!谢谢!
You can read the text file using readLines
, and convert the resulting vector into a matrix with 8 rows.您可以使用
readLines
读取文本文件,并将生成的向量转换为具有 8 行的矩阵。 When you transpose this matrix, all your fields will have their own column当你转置这个矩阵时,你所有的字段都会有自己的列
t(matrix(readLines("../mytext.txt"), nrow = 8))
#> [,1] [,2] [,3]
#> [1,] "1 Abigail Seltzer" "6019 8th St NW" "Washington, DC-20011"
#> [2,] "2 Danita Cole" "416 Shepherd St NW" "Washington, DC-20005"
#> [3,] "3 Landon James" "612 Quintana Pl NW" "Washington, DC-20011"
#> [,4]
#> [1,] "Not Employed Not Employed"
#> [2,] "Not Employed Not Employed"
#> [3,] "Social Inclusion Manager Brookfield Properties"
#> [,5] [,6]
#> [1,] "6019 8th St. NW" "Washington,DC-20011"
#> [2,] "416 Shepherd Street NW" "Washington,DC-20011"
#> [3,] "655 New York Ave NW" "Washington,DC-20000"
#> [,7] [,8]
#> [1,] "Credit Card 03/11/2022 $100.00 $100.00" ""
#> [2,] "Credit Card 03/11/2022 $100.00 $350.00" ""
#> [3,] "Credit Card 03/11/2022 $100.00 $100.00" ""
You could convert this into a data frame with as.data.frame
if you wished.如果您愿意,可以使用
as.data.frame
将其转换为数据框。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.