我有一个包含名称,年龄和公司的大型数据集。
file.txt:
name firstname1 lastname1
age 30
Company ABC Ltd
name firstname2 lastname2
age 28
Company XYZ Ltd
我需要编写一个函数,该函数将返回给定键属性的数据结构,并提供给定键的相应值。
例如
content <- parseFile("file.txt")
content[1]["name"] # "firstname1 lastname1"
content[1]["age"] # 30
content[1]["Company"] # "ABC Ltd"
content[2]["name"] # "firstname2 lastname2"
content[2]["age"] # 28
content[2]["Company"] # "XYZ Ltd"
到目前为止,我推断可以使用命名向量的列表或 可以使用对象列表。
还是有更好的方法来解决这个问题?
代码示例的解释将很有帮助
We can use
readLines
to get the data, create a delimiter withsub
and create a two column data.frame数据