【问题标题】:Generating column names to iterate over data frame in R生成列名以迭代 R 中的数据框
【发布时间】:2012-09-14 22:42:34
【问题描述】:

我是 R 新手,所以请多多包涵……我设法加载了一个数据框,并且标题有诸如“data$x.y.1”和“data$x.y.2”之类的列。我想以编程方式生成列名以提取每一列并绘制直方图。简而言之,我想构造'data$x.y' + n,我将在其中迭代n。我试过 cat 并得到:

as.name(cat("data$x.y", i, sep="."))
data$x.y.1Error in as.name(cat("data$x.y", i, sep = ".")) : 
invalid type/length (symbol/0) in vector allocation

【问题讨论】:

    标签: r


    【解决方案1】:
    names(data)
    

    应该得到你的列名

    如果你想要每列数据的直方图

    for(i in names(data)){
      png(paste(i,'.png',sep=""))
      hist(data[,which(names(data)==i)],
           main=paste(i,"some other stuff"),
           xlab=paste(i,"some more stuff") 
          )
      dev.off()
    }
    

    这将在您的工作目录中植入一堆名称类似于 x.y.1.png 的 .png 文件。

    【讨论】:

      【解决方案2】:
      df <- data.frame(x = rnorm(5), x1 = rnorm(5), x2 = rnorm(5))
      
      > df
                 x         x1          x2
      1  1.2222897  0.3904347 -0.05835815
      2 -1.7002094 -1.5195555 -0.79265835
      3  0.5570183 -1.3191265  0.26198408
      4 -0.2457016  0.1461557 -0.05567788
      5 -0.7689870 -0.6361940 -0.80780107
      
      library(plyr)
      library(reshape2)
      library(ggplot2)
      # melting the data.frame turns your data.frame from wide to tall
      # d_ply takes a data frame, applies a function each variable (in this case column) 
      # creates a plot, writes it to disk but returns nothing.
      d_ply(melt(df), .(variable), function(x){
          pl <- qplot(x$value, geom = "histogram", main = unique(x$variable))
          # Change the png to jpg, eps or pdf if you prefer a different format
          ggsave(pl, file = paste0(unique(x$variable),".png"))
          })
      
      # Now we can see that my working dir has 3 plots, each named after a column
      > dir(pattern = "png")
      [1] "x.png"  "x1.png" "x2.png"
      

      【讨论】:

        猜你喜欢
        • 2021-01-08
        • 2010-12-21
        • 2019-12-07
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-11-01
        • 2020-01-24
        • 1970-01-01
        相关资源
        最近更新 更多