2017-02-11 98 views
1

我有一個數據框/ tibble,看起來像下面這樣。多行分割列表[R]

# # A tibble: 2 × 3 
#  from_id    created_time  text 
#   <chr>     <chr> <list> 
# 1 10113538711 2017-02-10T23:33:01+0000 <chr [3]> 
# 2 10113538711 2017-02-10T05:41:39+0000 <chr [5]> 

我想從文本列出過行傳播list項目,所以它看起來像下面這樣。

# # A tibble: 2 × 3 
#   from_id    created_time        text 
#   <chr>     <chr>        <chr> 
# 1 10113538711 2017-02-10T23:33:01+0000 "earlier this week we received ..." 
# 1 10113538711 2017-02-10T23:33:01+0000 "lance payne's photo struck a c..." 
# 1 10113538711 2017-02-10T23:33:01+0000 "this is his story:" 
# 2 10113538711 2017-02-10T05:41:39+0000 "i'm melting, but extreme heat ..." 
# 2 10113538711 2017-02-10T05:41:39+0000 "place the container in an area..." 
# 2 10113538711 2017-02-10T05:41:39+0000 "please share far and wide." 
# 2 10113538711 2017-02-10T05:41:39+0000 "thank you." 
# 2 10113538711 2017-02-10T05:41:39+0000 "photo © tanya-dee johnson" 

我想試試tidy::separate()但這在我的用法中沒有用。我懷疑這是某種形式的拆分,或者分開,然後是gather()melt(),但是我的R詞彙目前讓我失望。

任何這方面的援助將不勝感激。

我的tibble DPUT。

> dput(df) 

structure(list(from_id = c("10113538711", "10113538711"), created_time = c("2017-02-10T23:33:01+0000", 
"2017-02-10T05:41:39+0000"), text = structure(list(c("earlier this week we received shocking photos of a turtle hatchling emerging beside a lump of coal at mackay's east point beach near hay point – the largest coal port alongside the great barrier reef.", 
"lance payne's photo struck a chord around the country.", "this is his story:" 
), c("i'm melting, but extreme heat causes significant stress particularly for all animals.", 
"place the container in an area where animals are protected from predators when drinking eg near a shrub or bush and keep your pets away from this area so that animals can drink undisturbed.", 
"please share far and wide.", "thank you.", "photo © tanya-dee johnson" 
)), class = c("get_sentences", "list"))), class = c("tbl_df", 
"tbl", "data.frame"), row.names = c(NA, -2L), .Names = c("from_id", 
"created_time", "text")) 

回答

2

我們可以使用unnest

library(tidyverse) 
unnest(df) 
+1

真快!我現在接受答案,所以不會讓我。我知道這是一個詞彙問題。 – Dan

+0

@Dan'reshape2'的'melt'和'tidyr'的'gather'用於從'wide'格式轉換爲'long'格式,並且不會觸及'list'列。 – akrun

+1

是的,我知道他們這樣做了,我只是認爲可能需要某種功能,會導致列表被拆分。顯然'unnest()'很容易。 – Dan