I'm having trouble reading a txt file in R. For example, the first row has 10112 and 1, the second row has number 1 first, and then 3 ratios. The next row has 8 numbers. The rows are all like this. In fact, the rows with number 10112, 10114, 10115, etc. are specific IDs. They represent the IDs of each unit. I want to do some further calculations for each unit, but I don't know if R is possible to read such txt files. How to do this so that the rows after 10112 belong to the unit 10112, then the rows after 10114 belong to the unit 10114? I use read.table(), and got the error below. Thanks for your help.

Error in scan(file = file, what = what, sep = sep, quote = quote, dec = dec, :
line 1 did not have 8 elements

Whenever you have a question on programming, first explore Stack Overflow. It's just great. I've provided an answer below based on a sample text file that I generated, but it is totally based on (just short of exact copy-paste) the accepted answer on this thread.

Thanks for your reply. I looked at the original thread, but I thought it is different to apply to my case. In my case, the number 10112, 10114, 10115, etc. each represents a unit. Then in the following lines, for example after 10112, 1 is group 1, 2 is group 2, the lines with 8 numbers after group1 and group2 represent the attributes of each group. There may be different number of groups in each unit. For example, in unit 10114, there are 3 groups: group1, group2 and group3, with their corresponding attributes following.
I want to extract all lines for each unit, no matter how many groups there are. There may be no group in one unit. How to do this then? Thanks.

Or maybe another way to look at this, start from the first row, if the 2nd number on row1 is 2, then the following 2 lines after "10112 2" belong to this unit. Then in the 4th row, the 2nd number is 5, so the following 5 rows after "10114 5" belong to this unit. Then next, in the row "10115 6", the 2nd number is 6, so the following 6 numbers belong to this unit, etc. The rows go on like this. Is it possible to do it in R? Thanks for your help.

This is not what I want to get, but thanks. I think Yarnabrina's answer could give me some hints.
I have adjusted my method. So if I read sample2 correctly using scan(), then I could change 10112, 10114, 10115 to some other corresponding values, and keep the values in this file. The values 10112, 10114, 10115 could be put into a list. I will think about it, thanks for your help.

I want to do something for each whole unit, for example like this in the following-Copy each unit to two more units and change the unit code for each copy. But it has to recognize the unit code first. The unit code is the first column in the rows with only two numbers, such as 10112, 10114, 10115 in sample2. Thanks for any suggestion.

I'm confused, in your original post you are asking to read the data into R as rows with a "unit" identifier (that's what I already did on my previous example), but now it seems like you want to manipulate a character string, since your desired output is not a dataframe with rows but another character string. If that is what you actually want to do, I recommend you to open a new topic for that, since the title in this one is going to be misleading for anyone trying to help.

Thanks for your solution. I want a list that is displayed as a string. I do not understand what this means?

In fact, my question is that I read in this file, and then copy and change all unit IDs to other values, but the question is in a new post now. And I want to output the result as a txt file, like the initial file format. Thanks for your help.