[英]How to save a data frame in R
根據這個問題的答案,您可以使用save()函數在R中保存數據框“foo”,如下所示:
save(foo,file="data.Rda")
這是數據框“df”:
> str(df)
'data.frame': 1254 obs. of 2 variables
$ text : chr "RT @SchmittySays: I love this 1st grade #science teacher from #Duluth http://t.co/HWDYFnIyqV #NSTA15 #AlbertEinstein #inspirat"| __truncated__ "RT @KVernonBHS: @smrtgrls would love Stellar Girls. Empowering female scientists rocks! #NSTA15 http://t.co/1ZU0yjVF67" "RT @leducmills: Leaving #SXSWedu to go straight to #NSTA15. There should be some sort of arbitrary conference-hopper social med"| __truncated__ "RT @KRScienceLady: Congrats to a wonderful colleague who helped #ngss Bcome reality, Stephen Pruitt, Distinguished Service to "| __truncated__ ...
$ group: Factor w/ 2 levels "narst","nsta": 2 2 2 2 2 2 2 2 2 2 ...
它似乎保存得很好:
> save(df, file = "~/downloads/df.Rda")
但事實證明只保存了對象的名稱:
> df1 <- load("~/downloads/df.Rda")
> str(df1)
chr "df"
我嘗試了在上面引用的同一個問題的另一個答案中建議的saveRDS()函數,該函數工作正常,但我想知道為什么save()不起作用。
您可能想在這里看一下這個問題: R數據格式:RData,Rda,Rds等 。
加載.rda
對象時,您將使用其原始名稱將所有對象加載到全局環境中。 您不能嘗試使用load
將對象分配給新名稱。
如果要保存以后可以加載不同名稱的對象,則應使用.rds
格式( saveRDS
和readRDS
)。 如果要在.rds
文件中保存多個對象,最簡單的解決方案是將所有對象放在列表中並僅保存列表。 如果在讀取.rds
后想要將列表的對象放在全局環境中,則可以使用list2env
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.