简体   繁体   English

有没有办法将文本文件的第 N 行移动到 R 中的特定列?

[英]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.

相关问题 有没有办法将列中的每隔一行移动到 R 中的新列中? - Is there a way to move every other row in a column into a new column in R? 如何创建新列并添加特定于 R Studio 中每一行的文本? - How do I create a new column and add text that is specific to each row in R studio? 提取R中DT每行的第n列,其中n是DT中行数的向量 - extracting the nth column of each row of a DT in R where n is a vector of the number of rows in the DT 申请 R 中的每一行和特定列以将数据插入数据库 - apply for each row and specific column in R to Insert data to database R-如何根据不同列中的值将特定列中的值移动到不同行 - R- How to move value in a specific column to a different row based on a value in a different column 编辑R中列中的每一行 - Editing each row in column in R 按第n列排序数据,获取第n行的行名,对每一列执行此操作 - Order the data by nth column, get rowname of the nth row, do this for each column 两组之间的绝对差异及其在 R 中每行的 95% 置信区间,并将其添加到特定列中的相应行 - absolute differences between 2 groups and their 95% confidence intervals in R for each row and add that to corresponding row in a specific column 如果元素是 R 中的特定值,如何替换列中的每个第 n 个元素 - How to replace every nth element in a column if the element is a specific value in R 在R中:为每一列保存带有不同分隔符的文本文件? - in R: save text file with different separators for each column?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM