我有一个文本文件,其中包含有关书名、作者姓名和出生国家的信息,这些信息以单独的行显示,如下所示:
Oscar Wilde
De Profundis
Ireland
Nathaniel Hawthorn
Birthmark
USA
James Joyce
Ulysses
Ireland
Walt Whitman
Leaves of Grass
USA
有没有办法将文本转换为数据框,这三个项目显示为不同的列:
ID Author Book Country
1 "Oscar Wilde" "De Profundis" "Ireland"
2 "Nathaniel Hawthorn" "Birthmark" "USA"
最佳答案
有内置函数可以处理这种数据:
data.frame(scan(text=xx, multi.line=TRUE,
what=list(Author="", Book="", Country=""), sep="\n"))
# Author Book Country
#1 Oscar Wilde De Profundis Ireland
#2 Nathaniel Hawthorn Birthmark USA
#3 James Joyce Ulysses Ireland
#4 Walt Whitman Leaves of Grass USA
关于r - 是否可以将文本文件中的行转换为列以获取数据框?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68371066/