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

问题描述 投票:0回答:1

目前在导入推文数据集时遇到问题,因此每个观察结果都在同一列中

这是导入之前的数据;每个推文都包含三个单元格,中间有一个空格。

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万行,因此不确定此解析问题是否是这里的关键。任何帮助表示赞赏!

r loading read.csv
1个回答
0
投票

也许类似的东西对您有用。

数据

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)

处理中

我确定您可以坚持使用数据表,但我是dplyr人。

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

基本上,我要抓取第一个字符(T,U,W)并将其存储到名为Column的变量中。我为字符串的其余部分添加了另一个名为Content的列,两端均修剪了空白。我还添加了ID列,因此我知道如何对3行的集群进行分组。

然后您基本上只需要按Column

read_rows %>%
  as_tibble() %>%
  rename(text = V1) %>%
  mutate(ID = rep(1:3, each = n() / 3),
         Column = str_sub(text, 1, 1),
         Content = str_trim(str_sub(text, 2))) %>%
  select(-text) %>%
  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"
© www.soinside.com 2019 - 2024. All rights reserved.