Reputation: 326
I currently have a list in R that looks like this:
[[1]]
# A tibble: 44 x 90
user_id status_id created_at screen_name text source display_text_wi…
<chr> <chr> <dttm> <chr> <chr> <chr> <dbl>
1 941080… 13270569… 2020-11-13 01:13:35 SenDougJon… "Luc… Twitt… 277
2 941080… 13270253… 2020-11-12 23:08:02 SenDougJon… "Our… Twitt… 288
3 941080… 13269906… 2020-11-12 20:49:52 SenDougJon… "Hav… Twitt… 74
4 941080… 13266836… 2020-11-12 00:30:09 SenDougJon… "Eve… Twitt… 230
5 941080… 13266610… 2020-11-11 23:00:09 SenDougJon… "We … Twitt… 122
6 941080… 13266399… 2020-11-11 21:36:36 SenDougJon… "I’v… Twitt… 154
7 941080… 13266081… 2020-11-11 19:30:15 SenDougJon… "Dur… Twitt… 283
8 941080… 13265892… 2020-11-11 18:14:52 SenDougJon… "On … Twitt… 187
9 941080… 13262990… 2020-11-10 23:01:58 SenDougJon… "For… Twitt… 198
10 941080… 13262799… 2020-11-10 21:45:56 SenDougJon… "Rou… Twitt… 164
# … with 34 more rows, and 83 more variables: reply_to_status_id <chr>,
# reply_to_user_id <chr>, reply_to_screen_name <chr>, is_quote <lgl>, is_retweet <lgl>,
# favorite_count <int>, retweet_count <int>, quote_count <int>, reply_count <int>,
# hashtags <list>, symbols <list>, urls_url <list>, urls_t.co <list>,
# urls_expanded_url <list>, media_url <list>, media_t.co <list>,
# media_expanded_url <list>, media_type <list>, ext_media_url <list>,
# ext_media_t.co <list>, ext_media_expanded_url <list>, ext_media_type <chr>,
# mentions_user_id <list>, mentions_screen_name <list>, lang <chr>,
# quoted_status_id <chr>, quoted_text <chr>, quoted_created_at <dttm>,
# quoted_source <chr>, quoted_favorite_count <int>, quoted_retweet_count <int>,
# quoted_user_id <chr>, quoted_screen_name <chr>, quoted_name <chr>,
# quoted_followers_count <int>, quoted_friends_count <int>, quoted_statuses_count <int>,
# quoted_location <chr>, quoted_description <chr>, quoted_verified <lgl>,
# retweet_status_id <chr>, retweet_text <chr>, retweet_created_at <dttm>,
# retweet_source <chr>, retweet_favorite_count <int>, retweet_retweet_count <int>,
# retweet_user_id <chr>, retweet_screen_name <chr>, retweet_name <chr>,
# retweet_followers_count <int>, retweet_friends_count <int>,
# retweet_statuses_count <int>, retweet_location <chr>, retweet_description <chr>,
# retweet_verified <lgl>, place_url <chr>, place_name <chr>, place_full_name <chr>,
# place_type <chr>, country <chr>, country_code <chr>, geo_coords <list>,
# coords_coords <list>, bbox_coords <list>, status_url <chr>, name <chr>, location <chr>,
# description <chr>, url <chr>, protected <lgl>, followers_count <int>,
# friends_count <int>, listed_count <int>, statuses_count <int>, favourites_count <int>,
# account_created_at <dttm>, verified <lgl>, profile_url <chr>,
# profile_expanded_url <chr>, account_lang <lgl>, profile_banner_url <chr>,
# profile_background_url <lgl>, profile_image_url <chr>
[[2]]
# A tibble: 49 x 90
user_id status_id created_at screen_name text source display_text_wi…
<chr> <chr> <dttm> <chr> <chr> <chr> <dbl>
1 224285… 13273273… 2020-11-13 19:07:49 SenatorBen… "Rec… Twitt… 284
2 224285… 13270535… 2020-11-13 00:59:54 SenatorBen… "In … Twitt… 278
3 224285… 13270335… 2020-11-12 23:40:29 SenatorBen… "Tun… Twitt… 274
4 224285… 13270267… 2020-11-12 23:13:36 SenatorBen… "Pre… Twitt… 268
5 224285… 13269869… 2020-11-12 20:35:08 SenatorBen… "I’m… Twitt… 283
6 224285… 13269530… 2020-11-12 18:20:37 SenatorBen… "I w… Twitt… 254
7 224285… 13269530… 2020-11-12 18:20:36 SenatorBen… "We … Twitt… 88
8 224285… 13266638… 2020-11-11 23:11:18 SenatorBen… "Thi… Twitt… 203
9 224285… 13265594… 2020-11-11 16:16:41 SenatorBen… "Thi… Twitt… 267
10 224285… 13263433… 2020-11-11 01:58:02 SenatorBen… "NEW… Twitt… 288
# … with 39 more rows, and 83 more variables: reply_to_status_id <chr>,
# reply_to_user_id <chr>, reply_to_screen_name <chr>, is_quote <lgl>, is_retweet <lgl>,
# favorite_count <int>, retweet_count <int>, quote_count <int>, reply_count <int>,
# hashtags <list>, symbols <list>, urls_url <list>, urls_t.co <list>,
# urls_expanded_url <list>, media_url <list>, media_t.co <list>,
# media_expanded_url <list>, media_type <list>, ext_media_url <list>,
# ext_media_t.co <list>, ext_media_expanded_url <list>, ext_media_type <chr>,
# mentions_user_id <list>, mentions_screen_name <list>, lang <chr>,
# quoted_status_id <chr>, quoted_text <chr>, quoted_created_at <dttm>,
# quoted_source <chr>, quoted_favorite_count <int>, quoted_retweet_count <int>,
# quoted_user_id <chr>, quoted_screen_name <chr>, quoted_name <chr>,
# quoted_followers_count <int>, quoted_friends_count <int>, quoted_statuses_count <int>,
# quoted_location <chr>, quoted_description <chr>, quoted_verified <lgl>,
# retweet_status_id <chr>, retweet_text <chr>, retweet_created_at <dttm>,
# retweet_source <chr>, retweet_favorite_count <int>, retweet_retweet_count <int>,
# retweet_user_id <chr>, retweet_screen_name <chr>, retweet_name <chr>,
# retweet_followers_count <int>, retweet_friends_count <int>,
# retweet_statuses_count <int>, retweet_location <chr>, retweet_description <chr>,
# retweet_verified <lgl>, place_url <chr>, place_name <chr>, place_full_name <chr>,
# place_type <chr>, country <chr>, country_code <chr>, geo_coords <list>,
# coords_coords <list>, bbox_coords <list>, status_url <chr>, name <chr>, location <chr>,
# description <chr>, url <chr>, protected <lgl>, followers_count <int>,
# friends_count <int>, listed_count <int>, statuses_count <int>, favourites_count <int>,
# account_created_at <dttm>, verified <lgl>, profile_url <chr>,
# profile_expanded_url <chr>, account_lang <lgl>, profile_banner_url <chr>,
# profile_background_url <chr>, profile_image_url <chr>
[[3]]
# A tibble: 48 x 90
user_id status_id created_at screen_name text source display_text_wi…
<chr> <chr> <dttm> <chr> <chr> <chr> <dbl>
1 278124… 13273169… 2020-11-13 18:26:41 SenBlument… "My … Twitt… 221
2 278124… 13270668… 2020-11-13 01:52:54 SenBlument… "Mul… Twitt… 236
3 278124… 13270511… 2020-11-13 00:50:15 SenBlument… "Whe… Twitt… 173
4 278124… 13269223… 2020-11-12 16:18:24 SenBlument… "Nat… Twitt… 218
5 278124… 13266073… 2020-11-11 19:26:57 SenBlument… "A r… Twitt… 241
6 278124… 13265817… 2020-11-11 17:45:00 SenBlument… "Gra… Twitt… 218
7 278124… 13265669… 2020-11-11 16:46:30 SenBlument… "Maj… Twitt… 182
8 278124… 13262942… 2020-11-10 22:42:50 SenBlument… "For… Twitt… 218
9 278124… 13262406… 2020-11-10 19:09:48 SenBlument… "As … Twitt… 264
10 278124… 13262406… 2020-11-10 19:09:48 SenBlument… "Tru… Twitt… 184
# … with 38 more rows, and 83 more variables: reply_to_status_id <chr>,
# reply_to_user_id <chr>, reply_to_screen_name <chr>, is_quote <lgl>, is_retweet <lgl>,
# favorite_count <int>, retweet_count <int>, quote_count <int>, reply_count <int>,
# hashtags <list>, symbols <list>, urls_url <list>, urls_t.co <list>,
# urls_expanded_url <list>, media_url <list>, media_t.co <list>,
# media_expanded_url <list>, media_type <list>, ext_media_url <list>,
# ext_media_t.co <list>, ext_media_expanded_url <list>, ext_media_type <chr>,
# mentions_user_id <list>, mentions_screen_name <list>, lang <chr>,
# quoted_status_id <chr>, quoted_text <chr>, quoted_created_at <dttm>,
# quoted_source <chr>, quoted_favorite_count <int>, quoted_retweet_count <int>,
# quoted_user_id <chr>, quoted_screen_name <chr>, quoted_name <chr>,
# quoted_followers_count <int>, quoted_friends_count <int>, quoted_statuses_count <int>,
# quoted_location <chr>, quoted_description <chr>, quoted_verified <lgl>,
# retweet_status_id <chr>, retweet_text <chr>, retweet_created_at <dttm>,
# retweet_source <chr>, retweet_favorite_count <int>, retweet_retweet_count <int>,
# retweet_user_id <chr>, retweet_screen_name <chr>, retweet_name <chr>,
# retweet_followers_count <int>, retweet_friends_count <int>,
# retweet_statuses_count <int>, retweet_location <chr>, retweet_description <chr>,
# retweet_verified <lgl>, place_url <chr>, place_name <chr>, place_full_name <chr>,
# place_type <chr>, country <chr>, country_code <chr>, geo_coords <list>,
# coords_coords <list>, bbox_coords <list>, status_url <chr>, name <chr>, location <chr>,
# description <chr>, url <chr>, protected <lgl>, followers_count <int>,
# friends_count <int>, listed_count <int>, statuses_count <int>, favourites_count <int>,
# account_created_at <dttm>, verified <lgl>, profile_url <chr>,
# profile_expanded_url <chr>, account_lang <lgl>, profile_banner_url <chr>,
# profile_background_url <chr>, profile_image_url <chr>
As you can see in the list, [[1]] is unique to the same userid and [[2]] is unique to the same userid and same for [[3]].
What I want to do, is to make a separate dataframe from each list within this larger list with the hope of eventually making each of those data frames a separate csv.
I am pretty new to the concept of lists in r so I was wondering if you all had suggestions for the best approach for this? Would I use lapply() and unlist() to essentially make each of the sublists their own data frame or use a for loop?
Upvotes: 0
Views: 45
Reputation: 326
I wrote the following code that answered my question based on the helpful comments above (THANK YOU!):
lapply(seq_along(list), function(i) data.table::fwrite(list[[i]], paste0("df", i, ".csv")))
Upvotes: 0