【发布时间】:2016-12-29 06:10:29
【问题描述】:
##Initialise empty dataframe
g <-data.frame(x= character(), y= character(),z=numeric())
## Loop through each columns and list out unique values (with the column name)
for(i in 1:ncol(iris))
{
a<-data.frame(colnames(iris)[i],unique(iris[,i]),i)
g<-rbind(g,a)
setNames(g,c('x','y','z'))
}
## write the output to csv file
write.csv(g,"1.csv")
输出的 CSV 文件是这样的
现在我想要的列标题不正确。我希望列标题分别为“x”、“y”、“z”。第一列也不应该在那里。
另外,如果您有任何其他有效的方法来做到这一点,请告诉我。谢谢!
【问题讨论】:
-
你可以像stackoverflow.com/a/6081514/3519000这样改变列名,你可以像stackoverflow.com/questions/7484325/…这样跳过行名。至于你的循环效率,我建议你先看看 StackOverflow。谢谢