入れ子になったループを使用してデータセットをサブセット化しようとしました。残念ながら、正しく動作していないように見えます。私は警告が2回出てループがうまく動作しません。入れ子のループ内R:置き換えられる項目の数が置き換えの長さの倍数ではありません
ここでは短いコード例です。提示されたデータは単なる例であり、実際のデータセットははるかに大きい:手作業で値を取り上げる解決策は実現不可能である。私は制限する場合は自分が私のデータセットでちょうど最初の要素に、 "通常の"(つまり、ネストされたNOT)ループがうまくいく
Warning messages:
1: In mysubset[[i]][j] <- mydata[[i]][which(mydata[[i]]$myInter == :
number of items to replace is not a multiple of replacement length
2: In mysubset[[i]][j] <- mydata[[i]][which(mydata[[i]]$myInter == :
number of items to replace is not a multiple of replacement length
3: In mysubset[[i]][j] <- mydata[[i]][which(mydata[[i]]$myInter == :
number of items to replace is not a multiple of replacement length
4: In mysubset[[i]][j] <- mydata[[i]][which(mydata[[i]]$myInter == :
number of items to replace is not a multiple of replacement length
5: In mysubset[[i]][j] <- mydata[[i]][which(mydata[[i]]$myInter == :
number of items to replace is not a multiple of replacement length
:
# #If I don't use a nested loop (by just using the first element in both "mydata" and "unique_test"), things seem to work out
# #But obviously, this is not really what I want to achieve (I can't just manually select every element in mydata and unique_test)
mysubset <- list()
for(i in 1:length(unique_test[[1]])){
#Select myvalues whose myInter data equals the one found in unique_test and assign them to a new subset
mysubset[[i]] <- mydata[[1]][which(mydata[[1]]$myInter == unique_test[[1]][i]),][["myvalue"]]
}
# #Generate example data
unique_test <- list()
unique_test[[1]] <- c(178.5, 179.5, 180.5, 181.5)
unique_test[[2]] <- c(269.5, 270.5, 271.5)
tmp_dataframe1 <- data.frame(myID = c(268, 305, 268, 305, 268, 305, 306),
myvalue = c(1.150343, 2.830392, 1.150343, 2.830392, 1.150343, 2.830392, 1.150343),
myInter = c(178.5, 178.5, 179.5, 179.5, 180.5, 180.5, 181.5))
tmp_dataframe2 <- data.frame(myID = c(144, 188, 196, 300, 301, 302, 303, 97),
myvalue = c(1.293493, 3.286649, 1.408049, 0.469219, 11.143147, 0.687355, 0.508603, 0.654335),
myInter = c(269.5, 269.5, 269.5, 270.5, 270.5, 271.5, 185.5, 186.5))
mydata <- list()
mydata[[1]] <- tmp_dataframe1
mydata[[2]] <- tmp_dataframe2
########################
# #Generate nested loop
mysubset <- list() #Define list
for(i in 1:length(unique_test)){
#Prepare list of lists
mysubset[[i]] <- NaN
for(j in 1:length(unique_test[[i]])){
#Select myvalues whose myInter data equals the one found in unique_test and assign them to a new subset
mysubset[[i]][j] <- mydata[[i]][which(mydata[[i]]$myInter == unique_test[[i]][j]),][["myvalue"]]
}
}
# #There are warnings and the nested loop is not really doing, what it is supposed to do!
Rは、次の警告を与えます
私は最初に適切な次元でリストを開始する必要がありますか?しかし、もし次元が私のデータセットのすべての要素で同じでないなら、私はそれをどうやって行うのですか?(それで、最初にlength()関数を使わなければなりません) mydata [[1]]はmydata [2]と同じ次元ではありません。以下のリンクで提示 そのための解決策は、このデータセットには適用されません。
Error in R :Number of items to replace is not a multiple of replacement length
Error in `*tmp*`[[k]] : subscript out of bounds in R
私はそれは私が欠けている明白な何かかなり確信しているが、私はそれを見つけることができません。どんな助けでも大歓迎です!
ループなしで同じことを達成するより良い方法がある場合は、サブセットの行に沿ってapply()や何かがあると確信しています。残念ながら、私はそれらを素早く実装するための選択肢には慣れていません。
ありがとうございました!これは問題を解決しました! – user6475