也许我错过了一些东西,这是微不足道的,如果是这样,我道歉。我尝试了许多不同的方法,每个方法都会产生自己独特的问题。
我有一个MIXED数据列表。数字,布尔值,字符串等。有些列是布尔值。有些是数字的。有些是字符串。
这是3行,24列。
这是一个简单的例子,每个都少了,但会演示我想要的东西。
public static void main(String[] args) throws ParseException {
SimpleDateFormat f = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
Date d = f.parse("2008-06-20 15:30:11.656"); // Format String to a dateobject with the format provided by the String.
SimpleDateFormat f2 = new SimpleDateFormat("MMMM dd, yyyy"); // MMMM for full month name
System.out.println(f2.format(d)); // Use the second format to create the desired format as String with d as input.
}
所以这被存储为变量“allMentions” typeof(allMentions)返回“list” 昏暗(allMentions)返回3 24,但在这个玩具示例中将是3 4
我希望尽可能不丢失我的数据类型...我想将此列表保存为CSV文件。
为此,我需要将其转换为数据框。
保存数据的“最佳”方法是什么,并且能够将其保存为.csv文件以供日后分析?
编辑1:被问到str(allMentions)
name boolVal quote postNum
Tom TRUE Howdy everyone! 1337
Tom FALSE Twitter is fun! 1338
Dara FALSE When it actually works lol. 24
下一步...
'data.frame': 3 obs. of 24 variables:
$ metadata :'data.frame': 3 obs. of 2 variables:
..$ iso_language_code: chr "en" "en" "en"
..$ result_type : chr "recent" "recent" "recent"
$ created_at : chr "Thu Oct 22 01:19:44 +0000 2015" "Thu Oct 22 01:15:46 +0000 2015" "Wed Oct 21 20:01:57 +0000 2015"
$ id : num 6.57e+17 6.57e+17 6.57e+17
etc with the rest of my variables and values
'data.frame': 3 obs. of 24 variables:
$ metadata :'data.frame': 3 obs. of 2 variables:
..$ iso_language_code: chr "en" "en" "en"
..$ result_type : chr "recent" "recent" "recent"
$ created_at : chr "Thu Oct 22 01:19:44 +0000 2015" "Thu Oct 22 01:15:46 +0000 2015" "Wed Oct 21 20:01:57 +0000 2015"
$ id : num 6.57e+17 6.57e+17 6.57e+17
$ id_str : chr "657003367575760896" "657002373307568129" "656923397985816576"
$ text : chr "@esorarad It worked, in case youre curious, since I keep bothering you." "@esorarad still testing!!" "@esorarad Hey Dara! Tweeting at you so I can test a program I'm making :P Hopeful game night in the near future!"
$ source : chr "<a href=\"http://twitter.com\" rel=\"nofollow\">Twitter Web Client</a>" "<a href=\"http://twitter.com\" rel=\"nofollow\">Twitter Web Client</a>" "<a href=\"http://twitter.com\" rel=\"nofollow\">Twitter Web Client</a>"
$ truncated : logi FALSE FALSE FALSE
$ in_reply_to_status_id : logi NA NA NA
$ in_reply_to_status_id_str: logi NA NA NA
$ in_reply_to_user_id : num 1.51e+09 1.51e+09 1.51e+09
$ in_reply_to_user_id_str : chr "1512420228" "1512420228" "1512420228"
$ in_reply_to_screen_name : chr "esorarad" "esorarad" "esorarad"
$ user :'data.frame': 3 obs. of 40 variables:
..$ id : num 75328338 75328338 75328338
..$ id_str : chr "75328338" "75328338" "75328338"
..$ name : chr "Thomas" "Thomas" "Thomas"
..$ screen_name : chr "uName" "uName" "uName"
..$ location : chr "Philadelphia PA" "Philadelphia PA" "Philadelphia PA"
..$ description : chr "" "" ""
..$ url : logi NA NA NA
..$ entities :'data.frame': 3 obs. of 1 variable:
.. ..$ description:'data.frame': 3 obs. of 1 variable:
.. .. ..$ urls:List of 3
.. .. .. ..$ : list()
.. .. .. ..$ : list()
.. .. .. ..$ : list()
..$ protected : logi FALSE FALSE FALSE
..$ followers_count : num 36 36 36
..$ friends_count : num 244 244 244
..$ listed_count : num 1 1 1
..$ created_at : chr "Fri Sep 18 16:56:20 +0000 2009" "Fri Sep 18 16:56:20 +0000 2009" "Fri Sep 18 16:56:20 +0000 2009"
..$ favourites_count : num 121 121 121
..$ utc_offset : num -18000 -18000 -18000
..$ time_zone : chr "Quito" "Quito" "Quito"
..$ geo_enabled : logi FALSE FALSE FALSE
..$ verified : logi FALSE FALSE FALSE
..$ statuses_count : num 616 616 616
..$ lang : chr "en" "en" "en"
..$ contributors_enabled : logi FALSE FALSE FALSE
..$ is_translator : logi FALSE FALSE FALSE
..$ is_translation_enabled : logi FALSE FALSE FALSE
..$ profile_background_color : chr "C0DEED" "C0DEED" "C0DEED"
..$ profile_background_image_url : chr "http://abs.twimg.com/images/themes/theme1/bg.png" "http://abs.twimg.com/images/themes/theme1/bg.png" "http://abs.twimg.com/images/themes/theme1/bg.png"
..$ profile_background_image_url_https: chr "https://abs.twimg.com/images/themes/theme1/bg.png" "https://abs.twimg.com/images/themes/theme1/bg.png" "https://abs.twimg.com/images/themes/theme1/bg.png"
..$ profile_background_tile : logi FALSE FALSE FALSE
..$ profile_image_url : chr "http://pbs.twimg.com/profile_images/563789490821410817/vB2dhVj1_normal.jpeg" "http://pbs.twimg.com/profile_images/563789490821410817/vB2dhVj1_normal.jpeg" "http://pbs.twimg.com/profile_images/563789490821410817/vB2dhVj1_normal.jpeg"
..$ profile_image_url_https : chr "https://pbs.twimg.com/profile_images/563789490821410817/vB2dhVj1_normal.jpeg" "https://pbs.twimg.com/profile_images/563789490821410817/vB2dhVj1_normal.jpeg" "https://pbs.twimg.com/profile_images/563789490821410817/vB2dhVj1_normal.jpeg"
..$ profile_link_color : chr "0084B4" "0084B4" "0084B4"
..$ profile_sidebar_border_color : chr "C0DEED" "C0DEED" "C0DEED"
..$ profile_sidebar_fill_color : chr "DDEEF6" "DDEEF6" "DDEEF6"
..$ profile_text_color : chr "333333" "333333" "333333"
..$ profile_use_background_image : logi TRUE TRUE TRUE
..$ has_extended_profile : logi FALSE FALSE FALSE
..$ default_profile : logi TRUE TRUE TRUE
..$ default_profile_image : logi FALSE FALSE FALSE
..$ following : logi FALSE FALSE FALSE
..$ follow_request_sent : logi FALSE FALSE FALSE
..$ notifications : logi FALSE FALSE FALSE
$ geo : logi NA NA NA
$ coordinates : logi NA NA NA
$ place : logi NA NA NA
$ contributors : logi NA NA NA
$ is_quote_status : logi FALSE FALSE FALSE
$ retweet_count : num 0 0 0
$ favorite_count : num 0 0 0
$ entities :'data.frame': 3 obs. of 4 variables:
..$ hashtags :List of 3
.. ..$ : list()
.. ..$ : list()
.. ..$ : list()
..$ symbols :List of 3
.. ..$ : list()
.. ..$ : list()
.. ..$ : list()
..$ user_mentions:List of 3
.. ..$ :'data.frame': 1 obs. of 5 variables:
.. .. ..$ screen_name: chr "esorarad"
.. .. ..$ name : chr "esorarad"
.. .. ..$ id : num 1.51e+09
.. .. ..$ id_str : chr "1512420228"
.. .. ..$ indices :List of 1
.. .. .. ..$ : num 0 9
.. ..$ :'data.frame': 1 obs. of 5 variables:
.. .. ..$ screen_name: chr "esorarad"
.. .. ..$ name : chr "esorarad"
.. .. ..$ id : num 1.51e+09
.. .. ..$ id_str : chr "1512420228"
.. .. ..$ indices :List of 1
.. .. .. ..$ : num 0 9
.. ..$ :'data.frame': 1 obs. of 5 variables:
.. .. ..$ screen_name: chr "esorarad"
.. .. ..$ name : chr "esorarad"
.. .. ..$ id : num 1.51e+09
.. .. ..$ id_str : chr "1512420228"
.. .. ..$ indices :List of 1
.. .. .. ..$ : num 0 9
..$ urls :List of 3
.. ..$ : list()
.. ..$ : list()
.. ..$ : list()
$ favorited : logi FALSE FALSE FALSE
$ retweeted : logi FALSE FALSE FALSE
$ lang : chr "en" "en" "en"
这是我的变量“allMentions”。我希望这个确切的表格保存为CSV格式。
dput(droplevels(head(allMentions)))
structure(list(metadata = structure(list(iso_language_code = c("en",
"en", "en"), result_type = c("recent", "recent", "recent")), .Names = c("iso_language_code",
"result_type"), row.names = c(NA, 3L), class = "data.frame"),
created_at = c("Thu Oct 22 01:19:44 +0000 2015", "Thu Oct 22 01:15:46 +0000 2015",
"Wed Oct 21 20:01:57 +0000 2015"), id = c(657003367575761024,
657002373307568000, 656923397985816960), id_str = c("657003367575760896",
"657002373307568129", "656923397985816576"), text = c("@esorarad It worked, in case youre curious, since I keep bothering you.",
"@esorarad still testing!!", "@esorarad Hey Dara! Tweeting at you so I can test a program I'm making :P Hopeful game night in the near future!"
), source = c("<a href=\"http://twitter.com\" rel=\"nofollow\">Twitter Web Client</a>",
"<a href=\"http://twitter.com\" rel=\"nofollow\">Twitter Web Client</a>",
"<a href=\"http://twitter.com\" rel=\"nofollow\">Twitter Web Client</a>"
), truncated = c(FALSE, FALSE, FALSE), in_reply_to_status_id = c(NA,
NA, NA), in_reply_to_status_id_str = c(NA, NA, NA), in_reply_to_user_id = c(1512420228,
1512420228, 1512420228), in_reply_to_user_id_str = c("1512420228",
"1512420228", "1512420228"), in_reply_to_screen_name = c("esorarad",
"esorarad", "esorarad"), user = structure(list(id = c(75328338,
75328338, 75328338), id_str = c("75328338", "75328338", "75328338"
), name = c("Thomas", "Thomas", "Thomas"
), screen_name = c("uName", "uName", "uName"), location = c("Philadelphia PA",
"Philadelphia PA", "Philadelphia PA"), description = c("",
"", ""), url = c(NA, NA, NA), entities = structure(list(description = structure(list(
urls = list(list(), list(), list())), .Names = "urls", row.names = c(NA,
3L), class = "data.frame")), .Names = "description", row.names = c(NA,
3L), class = "data.frame"), protected = c(FALSE, FALSE, FALSE
), followers_count = c(36, 36, 36), friends_count = c(244,
244, 244), listed_count = c(1, 1, 1), created_at = c("Fri Sep 18 16:56:20 +0000 2009",
"Fri Sep 18 16:56:20 +0000 2009", "Fri Sep 18 16:56:20 +0000 2009"
), favourites_count = c(121, 121, 121), utc_offset = c(-18000,
-18000, -18000), time_zone = c("Quito", "Quito", "Quito"),
geo_enabled = c(FALSE, FALSE, FALSE), verified = c(FALSE,
FALSE, FALSE), statuses_count = c(616, 616, 616), lang = c("en",
"en", "en"), contributors_enabled = c(FALSE, FALSE, FALSE
), is_translator = c(FALSE, FALSE, FALSE), is_translation_enabled = c(FALSE,
FALSE, FALSE), profile_background_color = c("C0DEED",
"C0DEED", "C0DEED"), profile_background_image_url = c("http://abs.twimg.com/images/themes/theme1/bg.png",
"http://abs.twimg.com/images/themes/theme1/bg.png", "http://abs.twimg.com/images/themes/theme1/bg.png"
), profile_background_image_url_https = c("https://abs.twimg.com/images/themes/theme1/bg.png",
"https://abs.twimg.com/images/themes/theme1/bg.png",
"https://abs.twimg.com/images/themes/theme1/bg.png"),
profile_background_tile = c(FALSE, FALSE, FALSE), profile_image_url = c("http://pbs.twimg.com/profile_images/563789490821410817/vB2dhVj1_normal.jpeg",
"http://pbs.twimg.com/profile_images/563789490821410817/vB2dhVj1_normal.jpeg",
"http://pbs.twimg.com/profile_images/563789490821410817/vB2dhVj1_normal.jpeg"
), profile_image_url_https = c("https://pbs.twimg.com/profile_images/563789490821410817/vB2dhVj1_normal.jpeg",
"https://pbs.twimg.com/profile_images/563789490821410817/vB2dhVj1_normal.jpeg",
"https://pbs.twimg.com/profile_images/563789490821410817/vB2dhVj1_normal.jpeg"
), profile_link_color = c("0084B4", "0084B4", "0084B4"
), profile_sidebar_border_color = c("C0DEED", "C0DEED",
"C0DEED"), profile_sidebar_fill_color = c("DDEEF6", "DDEEF6",
"DDEEF6"), profile_text_color = c("333333", "333333",
"333333"), profile_use_background_image = c(TRUE, TRUE,
TRUE), has_extended_profile = c(FALSE, FALSE, FALSE),
default_profile = c(TRUE, TRUE, TRUE), default_profile_image = c(FALSE,
FALSE, FALSE), following = c(FALSE, FALSE, FALSE), follow_request_sent = c(FALSE,
FALSE, FALSE), notifications = c(FALSE, FALSE, FALSE)), .Names = c("id",
"id_str", "name", "screen_name", "location", "description",
"url", "entities", "protected", "followers_count", "friends_count",
"listed_count", "created_at", "favourites_count", "utc_offset",
"time_zone", "geo_enabled", "verified", "statuses_count",
"lang", "contributors_enabled", "is_translator", "is_translation_enabled",
"profile_background_color", "profile_background_image_url",
"profile_background_image_url_https", "profile_background_tile",
"profile_image_url", "profile_image_url_https", "profile_link_color",
"profile_sidebar_border_color", "profile_sidebar_fill_color",
"profile_text_color", "profile_use_background_image", "has_extended_profile",
"default_profile", "default_profile_image", "following",
"follow_request_sent", "notifications"), row.names = c(NA,
3L), class = "data.frame"), geo = c(NA, NA, NA), coordinates = c(NA,
NA, NA), place = c(NA, NA, NA), contributors = c(NA, NA,
NA), is_quote_status = c(FALSE, FALSE, FALSE), retweet_count = c(0,
0, 0), favorite_count = c(0, 0, 0), entities = structure(list(
hashtags = list(list(), list(), list()), symbols = list(
list(), list(), list()), user_mentions = list(structure(list(
screen_name = "esorarad", name = "esorarad", id = 1512420228,
id_str = "1512420228", indices = list(c(0, 9))), .Names = c("screen_name",
"name", "id", "id_str", "indices"), class = "data.frame", row.names = 1L),
structure(list(screen_name = "esorarad", name = "esorarad",
id = 1512420228, id_str = "1512420228", indices = list(
c(0, 9))), .Names = c("screen_name", "name",
"id", "id_str", "indices"), class = "data.frame", row.names = 1L),
structure(list(screen_name = "esorarad", name = "esorarad",
id = 1512420228, id_str = "1512420228", indices = list(
c(0, 9))), .Names = c("screen_name", "name",
"id", "id_str", "indices"), class = "data.frame", row.names = 1L)),
urls = list(list(), list(), list())), .Names = c("hashtags",
"symbols", "user_mentions", "urls"), row.names = c(NA, 3L
), class = "data.frame"), favorited = c(FALSE, FALSE, FALSE
), retweeted = c(FALSE, FALSE, FALSE), lang = c("en", "en",
"en")), .Names = c("metadata", "created_at", "id", "id_str",
"text", "source", "truncated", "in_reply_to_status_id", "in_reply_to_status_id_str",
"in_reply_to_user_id", "in_reply_to_user_id_str", "in_reply_to_screen_name",
"user", "geo", "coordinates", "place", "contributors", "is_quote_status",
"retweet_count", "favorite_count", "entities", "favorited", "retweeted",
"lang"), row.names = c(NA, 3L), class = "data.frame")
答案 0 :(得分:0)
好的......我没有回答你的问题,因为我认为CSV文件不是正确的格式,但这里是JSON版本(我认为它可以保留结构,以备不时之需)。
保存数据
library(jsonlite)
dput(serializeJSON(allMentions), file="lala.json")
读入数据
library(jsonlite)
indata <- unserializeJSON(fromJSON("lala.json"))
给出了
metadata.iso_language_code metadata.result_type created_at
1 en recent Thu Oct 22 01:19:44 +0000 2015
2 en recent Thu Oct 22 01:15:46 +0000 2015
3 en recent Wed Oct 21 20:01:57 +0000 2015
id id_str
1 6.570034e+17 657003367575760896
2 6.570024e+17 657002373307568129
3 6.569234e+17 656923397985816576
和str(indata)
产生
'data.frame': 3 obs. of 24 variables:
$ metadata :'data.frame': 3 obs. of 2 variables:
..$ iso_language_code: chr "en" "en" "en"
..$ result_type : chr "recent" "recent" "recent"
$ created_at : chr "Thu Oct 22 01:19:44 +0000 2015" "Thu Oct 22 01:15:46 +0000 2015" "Wed Oct 21 20:01:57 +0000 2015"
$ id : num 6.57e+17 6.57e+17 6.57e+17
$ id_str : chr "657003367575760896" "657002373307568129" "656923397985816576"
$ text : chr "@esorarad It worked, in case youre curious, since I keep bothering you." "@esorarad still testing!!" "@esorarad Hey Dara! Tweeting at you so I can test a program I'm making :P Hopeful game night in the near future!"
$ source : chr "<a href=\"http://twitter.com\" rel=\"nofollow\">Twitter Web Client</a>" "<a href=\"http://twitter.com\" rel=\"nofollow\">Twitter Web Client</a>" "<a href=\"http://twitter.com\" rel=\"nofollow\">Twitter Web Client</a>"
$ truncated : logi FALSE FALSE FALSE
$ in_reply_to_status_id : logi NA NA NA
$ in_reply_to_status_id_str: logi NA NA NA
$ in_reply_to_user_id : num 1.51e+09 1.51e+09 1.51e+09
$ in_reply_to_user_id_str : chr "1512420228" "1512420228" "1512420228"
$ in_reply_to_screen_name : chr "esorarad" "esorarad" "esorarad"
$ user :'data.frame': 3 obs. of 40 variables:
..$ id : num 75328338 75328338 75328338
..$ id_str : chr "75328338" "75328338" "75328338"
..$ name : chr "Thomas" "Thomas" "Thomas"
..$ screen_name : chr "uName" "uName" "uName"
..$ location : chr "Philadelphia PA" "Philadelphia PA" "Philadelphia PA"
..$ description : chr "" "" ""
..$ url : logi NA NA NA
..$ entities :'data.frame': 3 obs. of 1 variable:
.. ..$ description:'data.frame': 3 obs. of 1 variable:
.. .. ..$ urls:List of 3
.. .. .. ..$ : list()
.. .. .. ..$ : list()
.. .. .. ..$ : list()
..$ protected : logi FALSE FALSE FALSE
..$ followers_count : num 36 36 36
..$ friends_count : num 244 244 244
..$ listed_count : num 1 1 1
..$ created_at : chr "Fri Sep 18 16:56:20 +0000 2009" "Fri Sep 18 16:56:20 +0000 2009" "Fri Sep 18 16:56:20 +0000 2009"
..$ favourites_count : num 121 121 121
..$ utc_offset : num -18000 -18000 -18000
..$ time_zone : chr "Quito" "Quito" "Quito"
..$ geo_enabled : logi FALSE FALSE FALSE
..$ verified : logi FALSE FALSE FALSE
..$ statuses_count : num 616 616 616
..$ lang : chr "en" "en" "en"
..$ contributors_enabled : logi FALSE FALSE FALSE
..$ is_translator : logi FALSE FALSE FALSE
..$ is_translation_enabled : logi FALSE FALSE FALSE
..$ profile_background_color : chr "C0DEED" "C0DEED" "C0DEED"
..$ profile_background_image_url : chr "http://abs.twimg.com/images/themes/theme1/bg.png" "http://abs.twimg.com/images/themes/theme1/bg.png" "http://abs.twimg.com/images/themes/theme1/bg.png"
..$ profile_background_image_url_https: chr "https://abs.twimg.com/images/themes/theme1/bg.png" "https://abs.twimg.com/images/themes/theme1/bg.png" "https://abs.twimg.com/images/themes/theme1/bg.png"
..$ profile_background_tile : logi FALSE FALSE FALSE
..$ profile_image_url : chr "http://pbs.twimg.com/profile_images/563789490821410817/vB2dhVj1_normal.jpeg" "http://pbs.twimg.com/profile_images/563789490821410817/vB2dhVj1_normal.jpeg" "http://pbs.twimg.com/profile_images/563789490821410817/vB2dhVj1_normal.jpeg"
..$ profile_image_url_https : chr "https://pbs.twimg.com/profile_images/563789490821410817/vB2dhVj1_normal.jpeg" "https://pbs.twimg.com/profile_images/563789490821410817/vB2dhVj1_normal.jpeg" "https://pbs.twimg.com/profile_images/563789490821410817/vB2dhVj1_normal.jpeg"
..$ profile_link_color : chr "0084B4" "0084B4" "0084B4"
..$ profile_sidebar_border_color : chr "C0DEED" "C0DEED" "C0DEED"
..$ profile_sidebar_fill_color : chr "DDEEF6" "DDEEF6" "DDEEF6"
..$ profile_text_color : chr "333333" "333333" "333333"
..$ profile_use_background_image : logi TRUE TRUE TRUE
..$ has_extended_profile : logi FALSE FALSE FALSE
..$ default_profile : logi TRUE TRUE TRUE
..$ default_profile_image : logi FALSE FALSE FALSE
..$ following : logi FALSE FALSE FALSE
..$ follow_request_sent : logi FALSE FALSE FALSE
..$ notifications : logi FALSE FALSE FALSE
$ geo : logi NA NA NA
$ coordinates : logi NA NA NA
$ place : logi NA NA NA
$ contributors : logi NA NA NA
$ is_quote_status : logi FALSE FALSE FALSE
$ retweet_count : num 0 0 0
$ favorite_count : num 0 0 0
$ entities :'data.frame': 3 obs. of 4 variables:
..$ hashtags :List of 3
.. ..$ : list()
.. ..$ : list()
.. ..$ : list()
..$ symbols :List of 3
.. ..$ : list()
.. ..$ : list()
.. ..$ : list()
..$ user_mentions:List of 3
.. ..$ :'data.frame': 1 obs. of 5 variables:
.. .. ..$ screen_name: chr "esorarad"
.. .. ..$ name : chr "esorarad"
.. .. ..$ id : num 1.51e+09
.. .. ..$ id_str : chr "1512420228"
.. .. ..$ indices :List of 1
.. .. .. ..$ : num 0 9
.. ..$ :'data.frame': 1 obs. of 5 variables:
.. .. ..$ screen_name: chr "esorarad"
.. .. ..$ name : chr "esorarad"
.. .. ..$ id : num 1.51e+09
.. .. ..$ id_str : chr "1512420228"
.. .. ..$ indices :List of 1
.. .. .. ..$ : num 0 9
.. ..$ :'data.frame': 1 obs. of 5 variables:
.. .. ..$ screen_name: chr "esorarad"
.. .. ..$ name : chr "esorarad"
.. .. ..$ id : num 1.51e+09
.. .. ..$ id_str : chr "1512420228"
.. .. ..$ indices :List of 1
.. .. .. ..$ : num 0 9
..$ urls :List of 3
.. ..$ : list()
.. ..$ : list()
.. ..$ : list()
$ favorited : logi FALSE FALSE FALSE
$ retweeted : logi FALSE FALSE FALSE
$ lang : chr "en" "en" "en"