我有以下数据框
df_Place:
Name|Places
----+-----------------------
abc |delhi
bcd |mumbai,delhi
cde |chennai,hyderabad,delhi
def |mumbai
efg |bangalore,mumbai
ghi |delhi,bangalore
我希望有一个矩阵形式的位置,所以我在操作下面做了
df_Place$matrix<-as.matrix(strsplit(df_Place$Place,","))
我得到数据框
Name|Places |matrix
----+-----------------------+------------------------------
abc |delhi |delhi
bcd |mumbai,delhi |c("mumbai","delhi")
cde |chennai,hyderabad,delhi|c("chennai","hyderabad","delhi")
def |mumbai |mumbai
efg |bangalore,mumbai |c("bangalore","mumbai")
ghi |delhi,bangalore |c("delhi","bangalore")
现在尝试将此写入csv write.csv(df_Place “tx.csv”) 我得到以下错误: .External2中的错误(C_writetable,x,file,nrow(x),p,rnames,sep,eol,: 'EncodeElement'中未实现的类型'list'
但如果我删除矩阵列,那么它会成功写入。
我知道这将是非常基本的,但有人可以解释这个背后的原因
答案 0 :(得分:1)
它与将矩阵(具有多个维度)写入df(其中多个cols没有维度(向量))有关。我发现这个解决方案有效(见Outputting a Dataframe in R to a .csv)
# First coerce the data.frame to all-character
df_Place2 = data.frame(lapply(df_Place, as.character), stringsAsFactors=FALSE)
# write file
write.csv(df_place2,"tx.csv")