首页 文章

从列表中提取并保存到csv

提问于
浏览
1

我正在努力做一些我知道应该简单的事情 .

我有一个像这样的数据帧列表:

a <- rep(1, 10)
b <- rep(3.6, 10)
foo1 <- cbind(a, b)

d <- rep(2, 8)
b <- rep(4.9, 8)
foo2 <- cbind(d, b)

data <- list(foo1, foo2)

我想通过索引或列名从每个数据框中提取第二列,并使用write.table并使用与数据帧相同的名称保存到csv文件 . 我尝试了很多东西--- for循环和 lapplysapply .

我收到各种错误消息,但主要是以下内容:

In if (file == "") file <- stdout() else if (is.character(file)) { : the condition has length > 1 and only the first element will be used

这是我无法解决的 .

我知道我没有正确索引 . 请帮帮我!

1 回答

  • 1

    您可以使用循环迭代 data 的字段:

    for (i in 1:length(data)) {
        col <- data[[i]][,2]
        fname <- paste("foo", i, ".csv", sep="")
        write.table(col,fname)
    }
    

    write.table 命令可能需要进行一些调整,直到您获得所需格式的数据 .

相关问题