在 R 中导入数据时修复列问题

Fixing Column Issue When Importing Data in R

目前在导入推文数据集以便每个观察都在一列中时遇到问题

这是导入前的数据;它为每条推文包含三个单元格,中间有一个空白 space。

T   2009-06-11 00:00:03
U   http://twitter.com/imdb
W   No Post Title

T   2009-06-11 16:37:14
U   http://twitter.com/ncruralhealth
W   No Post Title

T   2009-06-11 16:56:23
U   http://twitter.com/boydjones
W   listening to "Big Lizard - The Dead Milkmen" ♫ http://blip.fm/~81kwz
library(tidyverse)

tweets1 <- read_csv("tweets.txt.gz", col_names = F, 
                    skip_empty_rows = F)

这是输出:

Parsed with column specification:
cols(
  X1 = col_character()
)
Warning message:
“71299 parsing failures.
row col  expected    actual            file
 35  -- 1 columns 2 columns 'tweets.txt.gz'
 43  -- 1 columns 2 columns 'tweets.txt.gz'
 59  -- 1 columns 2 columns 'tweets.txt.gz'
 71  -- 1 columns 5 columns 'tweets.txt.gz'
107  -- 1 columns 3 columns 'tweets.txt.gz'
... ... ......... ......... ...............
See problems(...) for more details.
”
# A tibble: 1,220,233 x 1
   X1                                   
   <chr>                                
 1 "T\t2009-06-11 00:00:03"             
 2 "U\thttp://twitter.com/imdb"         
 3 "W\tNo Post Title"                   
 4 NA                                   
 5 "T\t2009-06-11 16:37:14"             
 6 "U\thttp://twitter.com/ncruralhealth"
 7 "W\tNo Post Title"                   
 8 NA                                   
 9 "T\t2009-06-11 16:56:23"             
10 "U\thttp://twitter.com/boydjones"    
# … with 1,220,223 more rows

唯一的问题是许多解析失败,其中问题 (tweets1) 显示 R 需要一列,但得到了多列。有想法该怎么解决这个吗?根据我的教授,我的输出应该为我提供 140 万行,所以不确定这个解析问题是否是这里的关键。感谢您的帮助!

也许这样的东西对你有用。

数据

data <- 'T   2009-06-11 00:00:03
U   http://twitter.com/imdb
W   No Post Title

T   2009-06-11 16:37:14
U   http://twitter.com/ncruralhealth
W   No Post Title

T   2009-06-11 16:56:23
U   http://twitter.com/boydjones
W   listening to "Big Lizard - The Dead Milkmen" ♫ http://blip.fm/~81kwz'

对于大文件,fread() 应该很快。 sep = NULL 表示基本上只是完整阅读。您将 input = data 替换为 file = "tweets.txt.gz"

library(data.table)

read_rows <- fread(input = data, header = FALSE, sep = NULL, blank.lines.skip = TRUE)

处理中

你可以留在 data.table,但我已经在 tidyverse 注意到你了。

library(dplyr)
library(stringr)
library(tidyr)

基本上我是抓取第一个字符(T、U、W)并将其存储到一个名为 Column 的变量中。我正在为字符串的其余部分添加另一个名为 Content 的列,两端修剪为白色 space。我还添加了一个 ID 列,所以我知道如何对 3 行的簇进行分组。

然后你基本上只是在 Column 上旋转。我不确定您是否需要这最后一步,因此请根据需要删除。

read_rows %>%
  mutate(ID = rep(1:3, each = n() / 3),
         Column = str_sub(V1, 1, 1),
         Content = str_trim(str_sub(V1, 2))) %>%
  select(-V1) %>%
  pivot_wider(names_from = Column, values_from = Content)

结果

# A tibble: 3 x 4
     ID T                   U                                W                                                                         
  <int> <chr>               <chr>                            <chr>                                                                     
1     1 2009-06-11 00:00:03 http://twitter.com/imdb          No Post Title                                                             
2     2 2009-06-11 16:37:14 http://twitter.com/ncruralhealth No Post Title                                                             
3     3 2009-06-11 16:56:23 http://twitter.com/boydjones     "listening to \"Big Lizard - The Dead Milkmen\" ♫ http://blip.fm/~81kwz"