Does anyone know why the third for statement is an error?
The code below is the complete code from creating each object to processing the for statement, and the output from the middle.
I think it's very simple, but I get an error in the third for statement with the same configuration.
If you know anyone, please let me know.
The environment is Windows 10.
av<-1:8#Create Each Vector
bv<-1:20
cv<-1:40
avm<-av%>%matrix(nrow=4)%>%as.data.frame()#Create Each Data Frame
bvm<-bv%>%matrix(nrow=4)%>%as.data.frame()
cvm<-cv%>%matrix(nrow=4)%>%as.data.frame()
name1<-letters [1:2] # preparation
name2<-letters [1:5]
name3<-letters [1:10]
colnames(avm)<-name1#Column name entry
colnames(bvm)<-name2
colnames(cvm)<-name3
avm;bvm;cvm#verify
for (i in 1:2) {
a<-avm%>%filter(avm[i]<avm[4,i])%>%select(name1[i])
a%>%print()
}
for (i in 1:5) {
a<-bvm%>%filter(bvm[i]<bvm[4,i])%>%select(name2[i])
a%>%print()
}
for (i in 1:10) {
a<-cvm%>%filter(cvm[i]<cvm[4,i])%>%select(name3[i])
a%>%print()
}
>avm;bvm;cvm#verify
ab
1 1 5
2 2 6
3 3 7
4 4 8
abcde
1 1 5 9 13 17
2 2 6 10 14 18
3 3 7 11 15 19
4 4 8 12 16 20
abc de fghij
1 1 5 9 13 17 21 25 29 33 37
2 2 6 10 14 18 22 26 30 34 38
3 3 7 11 15 19 23 27 31 35 39
4 4 8 12 16 20 24 28 32 36 40
>for(i in 1:2){
a<-avm%>%filter(avm[i]<avm[4,i])%>%select(name1[i])
a%>%print()
}
a
1 1
2 2
3 3
B
1 5
2 6
3 7
>for(i in 1:5){
a<-bvm%>%filter(bvm[i]<bvm[4,i])%>%select(name2[i])
a%>%print()
}
a
1 1
2 2
3 3
B
1 5
2 6
3 7
c
1 9
2 10
3 11
d
1 13
2 14
3 15
e
1 17
2 18
3 19
>for (i in 1:10) {
a<-cvm%>%filter(cvm[i]<cvm[4,i])%>%select(name3[i])
a%>%print()
}
Error: Problem with `filter() `input`..1`.
iInput`..1`is`cvm[i]<cvm[4,i]`.
x undefined columns selected
Run`rlang::last_error()`to see where the error occurred.
I think I should write it in the comment section because I'm a little careless about answering questions, but I can't do text shaping in the comment section, so I'll write it as an answer.
If you use tidyverse because you use %>%
, you will no longer use index-only variables such as i
, j
, idx
, etc., and you will not see the column name i
.
First, select
can also be a column index, so writing like name3(idx)
is not necessary in the first place.
require(tidyverse)#The original code does not appear in the questionnaire, but it clearly has to be read before it works.
cvm<-data.frame(matrix(1:40,nrow=4)%>%setNames(letters[1:10])
for (idx in 1:10) filter(cvm, cvm[idx]<cvm[4,idx])%>%select(idx)%>%print()
For filter
, you can also specify column indexes using cross
or filter_at
.
for(idx in 1:10)filter(cvm,across(idx,~.<.[4]))%>%select(idx)%>%print()
# However, filter_at is deprecated
for(idx in 1:10)filter_at(cvm,idx,~.<.[4])%>%select(idx)%>%print()
In addition, if you use map
or walk
instead of the for statement, this is what happens
walk (1:10, ~filter(cvm, across(.x, ~.<.[4]))%>%select(.x)%>%print)
Thank you very much, everyone.
The solution was presented in the comments, so I will summarize the other details.
Here's a summary.
Problems this time:
The loop variable i
was duplicated with the column name i
of the specified data frame
solutions:
Changing the Loop Variable in the for Statement (from i
to idx
)
>for (idx in 1:10){
+ a<-cvm%>%filter(cvm[idx]<cvm[4,idx])%>%select(name3[idx])
+ a%>%print()
+ }
a
1 1
2 2
3 3
B
1 5
2 6
3 7
c
1 9
2 10
3 11
d
1 13
2 14
3 15
e
1 17
2 18
3 19
f
1 21
2 22
3 23
g
1 25
2 26
3 27
h
1 29
2 30
3 31
i
1 33
2 34
3 35
j
1 37
2 38
3 39
© 2024 OneMinuteCode. All rights reserved.