将R哈希表写入CSV文件? [英] writing a R hashtable in to a CSV file?
问题描述
我想实现的是写一个R哈希表到一个csv文件excel(列像格式化文件 - 键在第一列和值在第二)。请考虑这个例子。 (散列表是由散列包创建的)
< hash>包含5个键值对。
pre>
1:4
2:NULL
3:NULL
4:3 1
5:1 4
当我使用这个
write.csv ,file =hash.csv,row.names = FALSE)
/ p>
as.data.frame.default(x [[i]],可选= TRUE)错误:
强制类结构(hash,package =hash)到数据框架
<所以我尝试从每个键和值创建一个数据框架,然后绑定它们:a <-as.data.frame key(hash))
b< -as.data.frame(values(hash))
data.frame中出错(`1` =4,`2` = NULL,`3` = NULL,`4` = c(3,1:
参数表示不同的行数:1,0,2
然后创建一个列表和一个向量:
a& hash)
b <-values(hash)
d< -cbind(a,b)
f <-as.data.frame(d)
ab
1 1 4
2 2 NULL
3 3 NULL
4 4 3,1
5 5 1,4
但是当我尝试写它:
write.csv f,file =hash.csv,row.names = FALSE)
.External2中的错误(C_writetable,x,file,nrow(x),p,rnames,sep,eol,:
未实现在EncodeElement中键入列表
因此有任何其他方法修复此错误?
提前谢谢。解决方案这可以帮助我。但是它还不是我的问题的答案,因为我正在寻找一个更好的解决方案。
a&
b <-values(hash)
d< -cbind(a,b)
L <-as.data.frame(d)
L.df < .frame(lapply(L,as.character),stringsAsFactors = FALSE)
write.csv(f.df,file =file.CSV,row.names = FALSE)
csv文件格式
#ab
#1 4
#2 NULL
#3 NULL
#4 c(3,1)
#5 c 1,4)
what i am trying to implement is to write an R hashtable to a csv file excel ( column like formatted file- keys in first column and values in the second). please consider this example. (the hash table is created by hash package)
<hash> containing 5 key-value pair(s). 1 : 4 2 : NULL 3 : NULL 4 : 3 1 5 : 1 4
when I simply use this
write.csv(hash, file = "hash.csv",row.names=FALSE)
it gives me this error
Error in as.data.frame.default(x[[i]], optional = TRUE) : cannot coerce class "structure("hash", package = "hash")" to a data.frame
so i try to create a dataframe from each key and values and then bind them:
a<-as.data.frame(keys(hash)) b<-as.data.frame(values(hash)) Error in data.frame(`1` = "4", `2` = NULL, `3` = NULL, `4` = c("3", "1" : arguments imply differing number of rows: 1, 0, 2
then to create a list and a vector :
a<-keys(hash) b<-values(hash) d<-cbind(a,b) f<-as.data.frame(d) a b 1 1 4 2 2 NULL 3 3 NULL 4 4 3, 1 5 5 1, 4
but then when I try to write it:
write.csv(f, file = "hash.csv",row.names=FALSE) Error in .External2(C_writetable, x, file, nrow(x), p, rnames, sep, eol, : unimplemented type 'list' in 'EncodeElement'
so is there any other way to do that or any way to fix this error ? thank you in advance.
解决方案this somehow help me. but it is not yet the answer of my question as I was looking for a better solution.
a<-keys(hash) b<-values(hash) d<-cbind(a,b) L<-as.data.frame(d) L.df <- data.frame(lapply(L, as.character), stringsAsFactors=FALSE) write.csv(f.df, file ="file.CSV",row.names=FALSE) csv file format # a b # 1 4 # 2 NULL # 3 NULL # 4 c("3", "1") # 5 c("1", "4")
这篇关于将R哈希表写入CSV文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!