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)
}
Ausgang:mit lapply Funktion und Liste in 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"
Erwartung:
[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"
Wie konnte ich verbessern? Ich hoffe, SQL auszuführen, um ein neues Dataset zu erstellen, was ich will, aber es ist nicht erfolgreich, und ich kann angeben, den Index der Liste zu wissen, die sich auf die SQL-Anweisung bezieht. Gibt es eine andere einfache Methode?
Ich habe eine der Methoden ausprobiert.
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)
})
#}
Ausgang:
[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"
aber es wird nicht den Datensatz finden SQL auszuführen.
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
)
Sind Sie versuchen, die Zeilen aller Datenrahmen zusammen mit zusätzlichen Spalten für den Index in 'my.list' und der Zeilenanzahl von jedem zu binden? 'do.call (" rbind ", lapply (seq_along (meine.liste), funktion (i) cbind (Index = i, Anzahl = nrow (meine.liste [[i]]), meine.liste [[i]]))) ' –