4

これは二重バレルのRの質問です。データ セット (.csv) のフォルダーがあり、分析の前に変更する必要があります。各データセットは、次のような 1X10 行列です。

1 2 3 4 5 6 7 8 9 10

対角線に 1 を挿入して、次の 5X5 マトリックスに変換する必要があります。

1
1 1 
2 3 1
4 5 6 1
7 8 9 10 1

フォルダー内の複数のファイルでこの変換を行うにはどうすればよいですか?

4

1 に答える 1

7

これを試して:

dir.in  <- "aaa"  # replace with your own input dir
dir.out <- "bbb"  # replace with your own output dir

files.in  <- list.files(dir.in, full.names = TRUE)
files.out <- file.path(dir.out, basename(files.in))

data.in <- lapply(files.in, scan, sep = ",")

mat.out <- lapply(data.in, function(x){ M <- diag(1, 5)
                                        M[upper.tri(M)] <- x
                                        t(M) })

mapply(write.csv, mat.out, files.out, col.names = FALSE, row.names = FALSE)
于 2013-02-03T21:55:10.403 に答える