大きなテキスト文字列をデータフレームに変換するのに問題があります。私はまだこの単純なタスクを理解することができませんでした。あなたの助けを期待しています。
x <- "1 apple 200 blueberry 3000 pear 4400 raspberry"
これを次のようなデータフレームに変換したいと思います。
id name
1 apple
200 blueberry
30000 pear
4400 raspberrry
私たちは、使用することができgsub
てread.table
read.table(text=gsub("(?<=[a-z])\\s+", "\n", x, perl=TRUE),
header=FALSE, col.names = c("id", "name"))
# id name
#1 1 apple
#2 200 blueberry
#3 3000 pear
#4 4400 raspberry
またはと fread
library(data.table)
fread(gsub("(?<=[a-z])\\s+", "\n", x, perl=TRUE), col.names = c("id", "name"))
またはgsub
、col.names
withを指定することにより、これがなくても機能しますread.table
read.table(text=x,col.names=c('ID','Name'))
# ID Name
#1 1 apple
#2 200 blueberry
#3 3000 pear
#4 4400 raspberry
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加