私はRで10個のリスト(files1
、files2
、files3
、... files10
)で作業しています。各リストには複数のデータフレームが含まれています。R - forループのリストの名前を使用
ここで、各リストの各データフレームからいくつかの値を抽出します。
私がループので
nt = c("A", "C", "G", "T")
for (i in files1) {
for (j in nt) {
name = paste(j, i, sep = "-") # here I want as output name = "files1-A". However this doesn't work. How can I get the name of the list "files1"?
colname = paste("percentage", j, sep = "") # here I was as output colname = percentageA. This works
assign(name, unlist(lapply(i, function(x) x[here I want to use the column with the name "percentageA", so 'colname'][x$position==1000])))
}
}
に使用するつもりだった、私はトラブルがリストの名前を使用して、変数に割り当てています。
私は最初のリストをループしているだけですが、すぐにすべてのリストをループすることもできますか?
つまり、以下のコードをforループにどのように入れることができますか?
A_files1 = unlist(lapply(files1, function(x) x$percentageA[x$position==1000]))
C_files1 = unlist(lapply(files1, function(x) x$percentageC[x$position==1000]))
G_files1 = unlist(lapply(files1, function(x) x$percentageG[x$position==1000]))
T_files1 = unlist(lapply(files1, function(x) x$percentageT[x$position==1000]))
A_files2 = unlist(lapply(files2, function(x) x$percentageA[x$position==1000]))
C_files2 = unlist(lapply(files2, function(x) x$percentageC[x$position==1000]))
G_files2 = unlist(lapply(files2, function(x) x$percentageG[x$position==1000]))
T_files2 = unlist(lapply(files2, function(x) x$percentageT[x$position==1000]))
....
A_files10 = unlist(lapply(files10, function(x) x$percentageA[x$position==1000]))
C_files10 = unlist(lapply(files10, function(x) x$percentageC[x$position==1000]))
G_files10 = unlist(lapply(files10, function(x) x$percentageG[x$position==1000]))
T_files10 = unlist(lapply(files10, function(x) x$percentageT[x$position==1000]))
'names(fileS1)'は 'NULL'を返しますか? –
@ joel.wilson:はいそれはあります – user1987607
サンプルデータを投稿することは素晴らしいでしょう。 [再現可能な例を作る方法]を参照してください(http:// stackoverflow。com/questions/5963269/how-to-make-a-great-r-reproducible-example/5965451#5965451)を参照してください。一般に、複数のファイルを読み込むために、私は1つのファイルからデータフレームを返す関数(variable1、variable2)を作成します。次に、 'group_by(variable1、variable2)' 'do(myfunction(。$ variable1、。$ variable2))'で 'dplyr'パッケージを使って複数のファイルを読み込みます。これは単一のデータフレーム内のすべてのデータを取得するのに適しています。 –