d1 <- data.frame(col_one = c(1,2,3),col_two = c(4, 5, 6))
d2 <- data.frame(col_one = c(1, 1, 1), col_two = c(6, 5, 4))
d3 <- data.frame(col_one = c(7, 1, 1), col_two = c(8, 5, 4))
my.list <- list(d1, d2,d3)
for (i in 1:3) {
table<- lapply(my.list, function(data, count) {
sql <-
#sqldf(
paste0(
"select *,count(col_one) from data where col_one = ",
count," group by col_one"
)
#)
print(sql)
},
count = i)
}
出力lapply機能とリストを使用して:Rに
[1] "select *,count(col_one) from data where col_one = 1 group by col_one"
[1] "select *,count(col_one) from data where col_one = 1 group by col_one"
[1] "select *,count(col_one) from data where col_one = 1 group by col_one"
[1] "select *,count(col_one) from data where col_one = 2 group by col_one"
[1] "select *,count(col_one) from data where col_one = 2 group by col_one"
[1] "select *,count(col_one) from data where col_one = 2 group by col_one"
[1] "select *,count(col_one) from data where col_one = 3 group by col_one"
[1] "select *,count(col_one) from data where col_one = 3 group by col_one"
[1] "select *,count(col_one) from data where col_one = 3 group by col_one"
が期待:
[1] "select *,count(col_one) from data where col_one = 1 group by col_one"
[1] "select *,count(col_one) from data where col_one = 2 group by col_one"
[1] "select *,count(col_one) from data where col_one = 3 group by col_one"
どのように改善できますか?私は、私が望む新しいデータセットを作成するためにSQLを実行することを望みますが、成功していません。SQLステートメントに関連するリストのインデックスを知ることができます。別の簡単な方法がありますか?
私はこの方法の1つを試しました。
d1 <- data.frame(col_one = c(1,2,3),col_two = c(4, 5, 6))
d2 <- data.frame(col_one = c(3, 2, 1), col_two = c(6, 5, 4))
d3 <- data.frame(col_one = c(7, 2, 1), col_two = c(8, 5, 4))
my.list <- list(d1, d2,d3)
seq_along(x)
#for (i in 1:3) {
table<- lapply(seq_along(my.list), function(index) {
sql <-
sqldf(
paste0(
"select *,count(col_one) from my.list where col_one = ",
index," group by col_one"
)
)
print(sql)
})
#}
出力:
[1] "select *,count(col_one) from my.list where col_one = 1 group by col_one"
[1] "select *,count(col_one) from my.list where col_one = 2 group by col_one"
[1] "select *,count(col_one) from my.list where col_one = 3 group by col_one"
しかし、それはSQLを実行するためのデータセットを見つけることができません。
d1 <- data.frame(col_one = c(1,2,3),col_two = c(4, 5, 6))
d2 <- data.frame(col_one = c(1, 1, 1), col_two = c(6, 5, 4))
d3 <- data.frame(col_one = c(7, 1, 1), col_two = c(8, 5, 4))
my.list <- list(d1, d2,d3)
table<- mapply(function(data, count) {
sql <-
sqldf(
paste0(
"select *,count(col_one) from data where col_one = ",
count," group by col_one"
)
)
print(sql)
}, my.list, 1
)
あなたは 'my.list'のインデックスと各の行数のために追加の列と一緒にすべてのデータフレームの列をバインドしようとしています。これは、結果をフィルタリングすることによって達成することができますか? (my.list [[i]])、my.list [[i]] do.call( "rbind"、lapply(seq_along(my.list)、function(i)cbind) ))) ' –