【问题标题】:Build data frame from multiple CSVs in R从 R 中的多个 CSV 构建数据框
【发布时间】:2014-08-12 22:56:34
【问题描述】:

(:

我对 R 了解不多,但我需要使用它从数据集合中绘制一组箱线图。

我有一组代表一组二维数据的 .csv 文件。它们包含以下列:

i:矩阵的行

j:矩阵的列

VBoot:矩阵的一个属性

我的数据是 128 x 128,但 .csv 只包含非零属性的索引。

我必须为这些文件中的每一个并排绘制一个箱线图。

这是我的方法:

library(ggplot2)
library(reshape)

# Set the directory to read the files
setwd("/Users/me/data/CSV/")

operatorProperty <- function(operator, property, degrees, m, n)
{
    p <- list()
    for (degree in degrees)
    {
        file <- paste(c(degree, operator, property, ".csv"), collapse="")
        data <- read.csv(file, header=TRUE, sep=" ", dec=".") 
        # Create an array m * n to fill with the data
        b <- vector(mode="double", length=(m*n))
        # Rebuild the complete data to properly build the box plot
        b[data$i * m + data$j] = sqrt(data$VBoot)
        p <- append(p, list(b))
    }
    p
}

到目前为止,我刚刚创建了一个列表来插入每个 ensamble 的数据。

那么,我虽然应该建立一个data.frame

min_degree = 0
max_degree = 45
delta = 5
m = 128
n = 128

degrees <- seq(min_degree, max_degree, delta)
property <- "VBoot"
operator <- "Prewitt"
Sobel <- operatorProperty(operator, property, degrees, m, n)

df <- data.frame(degrees, Sobel)
df2<- melt(data=df,id.vars="degrees")
  
p <- ggplot(df2, aes(x=degrees,y=value,colour=variable)) +
    geom_boxplot() +
    theme(legend.title=element_blank()) +
    xlab(expression(theta)) +
    ylab("Bootstrap Variance")

但是,我无法构建 data.frame。我不知道该怎么做。数据示例可以在here找到。

提前谢谢你。

【问题讨论】:

  • 你希望最终的情节是什么样子的?您是否只想为每个文件中的非零值制作箱线图?或者你想让所有的零也算在内?你打算根据什么上色?
  • 我只是想获取每个数据,重建它(计算 .csv 中没有的零)并并排显示每个数据的箱线图,如下所示:@ 987654322@

标签: r csv dataframe boxplot


【解决方案1】:

好的。好吧,我必须更改一些东西才能使其与示例数据一起使用。这是设置

m = 128
n = 128

operatorProperty <- function(operator, property, degrees, m, n)
{
    Map(function(degree) {
        file <- paste(c(degree, operator, property, ".csv"), collapse="")
        data <- read.table(file, header=TRUE, dec=".") 
        # Create an array m * n to fill with the data
        b <- vector(mode="double", length=(m*n))
        # Rebuild the complete data to properly build the box plot
        b[data$i * m + data$j] = sqrt(data[[property]])
        b
    }, degrees)
}


degrees <- c('00','05')
property <- "MSE"
operator <- "Prewitt"
Sobel <- operatorProperty(operator, property, degrees, m, n)

使用这种修改形式,Sobel 是一个列表,其中包含与不同程度相对应的命名元素。我们可以把它变成一个data.frame并用

df2<- melt(data.frame(Sobel, check.names=F))

p <- ggplot(df2, aes(x=variable,y=value,colour=variable)) +
    geom_boxplot() +
    theme(legend.title=element_blank()) +
    xlab(expression(theta)) +
    ylab("Bootstrap Variance")

这看起来很有趣,因为你有很多零。您所有的非零条目都被标记为异常值。

但即使我们没有很好地命名Sobel 列表,它基本上也是一个包含两个向量的列表(每个度数一次)

list(c(0,0,0,0, ...), c(0,0,0,0,...))

如果您想将其与度数合并并转换为 data.frame,则可能是另一种选择

do.call(rbind, Map(cbind.data.frame, degrees, Sobel))

【讨论】:

  • 谢谢! (:但是,我尝试创建一个列表列表并将其映射到 data.frame。什么不正确(只是为了了解我的错误)?
  • 您的data.frame(degrees, Sobel) 线路错误。您将行数据与列数据混合在一起。 Sobel 是一个向量列表,每个向量的长度 m*ndegrees 要短得多,所以它正在被回收。
  • 是的。我试图做的是建立一个以度数作为行IDS的表,并将Sobel列表(女巫也是10号)作为“内容”。
猜你喜欢
  • 2020-07-07
  • 2018-06-11
  • 1970-01-01
  • 2023-03-21
  • 2016-08-11
  • 1970-01-01
  • 1970-01-01
  • 2021-12-09
  • 1970-01-01
相关资源
最近更新 更多