从列表中读取数字并使用与读取的数字对应的列创建 csv 文件

Read numbers from a list and create csv file with the columns that correspond to the numbers read

我有一个数据集保存在一个名为 'extremes'(30 列和 2000 行)的 csv 文件中。我执行聚类分析并使用 capture.output 来保存 输出到一个csv文件中。具体来说,我这样做:

    capture.output(inf,file="Clusters.csv", append=TRUE)

其中 'inf' 是一个函数,returns 分析输出。'inf' 是一个列表。

我保存在 csv 文件(称为 'Clusters.csv')中的输出如下(显示在 R 控制台中):

$assign
 [1] 1 2 3 1 1 1 1 2 1 4 1 4 1 2 4 2 3 5 4 1 2 2 2 1 1 1 1 1 1 1

$list
$list$cluster.1
 [1]  1  4  5  6  7  9 11 13 20 24 25 26 27 28 29 30

$list$cluster.2
[1]  2  8 14 16 21 22 23

$list$cluster.3
[1]  3 17

$list$cluster.4
[1] 10 12 15 19

$list$cluster.5
[1] 18


$num
cluster.1 cluster.2 cluster.3 cluster.4 cluster.5 
   16         7         2         4         1 

根据分析,我还得到一个名为 'NumberClusters' 的参数,它指示最佳聚类数(对于这个特定数据集,它的值为 2)。

我想要实现的是从 csv 文件 'extremes' 中读取构成第一个簇的特定列(即 1 4 5 6 7 9 11 13 20 24 25 26 27 28 29 30 ) 并将它们保存在 data.frame 中(并且可能将它们存储在名为 'Cluster1' 的 csv 文件中,然后从 csv 文件 'extremes' 中读取构成第二个簇的特定列(即, 2 8 14 16 21 22 23) 并将它们保存在 data.frame 中(也许在名为 'Cluster2' 的 csv 文件中)。然后我可以使用两个数据集 'Cluster1' 和 'Cluster2'。 我认为,我的主要问题是找到一种方法来从文件中读取构成每个集群的列(例如,对于集群 1,列:1 4 5 6 7 9 11 13 20 24 25 26 27 28 29 30) 'Clusters.csv'。我相信我将能够使用

读取文件 'extremes.csv' 中这些列中包含的数据
read.xls("extremes.csv")[c(1  4  5  6  7  9 11 13 20 24 25 26 27 28 29 30])     

我也曾尝试使用包 'xlsx' 但没有取得任何成果。

任何帮助将不胜感激,因为我已经坚持了一段时间了。

我的数据看起来像这样(这是一个小样本;实际上我有 30 列(金融指数)和 2019 行(每日 returns)。希望这对您有所帮助。

Food    Beer    Smoke   Games   Books   Hshld   Clths
0.57    1.23    1.19    0.54    -0.19   0.31    0.52
0.48    0.57    -0.89   -0.23   -0.25   0.29    -0.26
-0.55   -0.75   -0.8    -0.41   -0.2    -0.29   -0.61
 0.6    -0.1    0.31    1.16    1.14    0.74    0.72
-0.44   -1.34   -1.73   -0.16   0.22    -0.97   -0.96
-0.25   -0.21   -0.07   -0.73   -0.4    -0.56   -0.8
0.11    -0.94   -0.3    -0.38   -0.07   -0.38   -0.24
-1.34   -2.12   -1.54   -1.52   -0.68   -1.72   -1.91

我 运行 你的代码(你的模拟示例)我得到

> cluster1
Null data.table (0 rows and 0 cols)

cluster2 相同。

然后我 运行 使用我的数据集执行以下操作并得到相同的消息(即 Null data.table(0 行和 0 列)。

output <- read.csv("Clusters.csv", header = TRUE)
output <- list()
cluster.data <- matrix(extremes, nrow = 2019, ncol = 30, byrow = TRUE) 
DT <- as.data.table(cluster.data)
cluster1 <- DT[, c(output$list$cluster1), with = FALSE]
cluster1
cluster2 <- DT[, c(output$list$cluster2), with = FALSE]
cluster2

我怀疑我完全错了。

我运行没有输出的代码<-list()。即:

编辑:我认为这是因为我们没有得到正确的 output$list$cluster2 名称。尝试 output$list$cluster.2。我对下面的块进行了更改。请尝试:

output <- read.csv("Clusters.csv", header = TRUE)
# take a look at output
output

cluster.data <- matrix(extremes, nrow = 2019, ncol = 30, byrow = TRUE) 
DT <- as.data.table(cluster.data)
cluster1 <- DT[, c(output$list$cluster.1), with = FALSE]
cluster1
cluster2 <- DT[, c(output$list$cluster.2), with = FALSE]
cluster2

编辑:我们快到了!请尝试打印出 outputoutput$list$cluster.1 以及 str(output$list$cluster.2) 以查看其分类方式。最后,如果这不起作用,请在 output 上使用 dput 到文件并在 Notepad/text 编辑器中查看它。 dput 将数据写入 R 命令以重新创建。 Post 这样我们就可以检查输出了。

没有你的数据块有点棘手。如果您不熟悉此包,请查看data.table cheatsheet

假设您的列是标准列,因此没有名称 V1 V2。让我们隔离你的两个块,这样你就可以把它们保存下来。

library(data.table)

# mini mockup example using just first 5 columns
output <- list()
output$list$cluster.1 <- c(1,4,5)
output$list$cluster.2 <- c(2)
# EDIT: Kostas you would do this with your data
#  "output I save in the csv file (called 'Clusters.csv')"
# get the output structure back
# output <- read.csv("Clusters.csv", header = TRUE)
# Then the code will read your list results

# mockup of your data using a to e so we can see how columns selected
#   its simply two lines of repeated a b c d e
cluster.data <- matrix(letters[1:5], nrow = 2, ncol = 5, byrow = TRUE) 

#assuming you want the column names will just be default V1 V2...
#  cluster 1 we would expect it to look like this
#  headings     V1 V4 V5
#  data         a d e 
#  data line 2  a d e 


# turn it into a data.table
#   you would read your data in as csv 
#   data <- as.data.table(read.csv("yourfile.csv")) etc.
DT <- as.data.table(cluster.data)

# subset data to cluster 1
cluster1 <- DT[, c(output$list$cluster.1), with = FALSE]

   V1 V4 V5
1:  a  d  e
2:  a  d  e

# likewise for 2
cluster2 <- DT[, c(output$list$cluster.2), with = FALSE]

   V2
1:  b
2:  b

注意我在 data.table 中使用 with = FALSE 以便调用第 4 列而不是称为 4 的列。

然后你会把这些块保存下来。参见 'write.table' 或 'write.csv'。在提示符下键入 ?write.table 以获得帮助。

您可以 "parameterize" 使用以下方法获得不同的簇长度: as.name(paste0("cluster.", as.character(i))) 当 i = 3 时得到 cluster.3

希望对您有所帮助!

后期编辑:Kostas 我看到你的输出数据现在称为 cluster.1 而不是我原来的 cluster1 所以我编辑了上面的代码。 $list$cluster.1