why can't subset data frame by some keys?
up vote
0
down vote
favorite
such data frame as follows:
df <- data.frame(yr = rep(2000:2017,each=46),
dayorder = rep(seq(1,365,8),time=18),
fid = rep(1:46,time=18),
Value = runif(46*18)
)
when I sieve by keys:
df[which(dayorder==9),]
yr dayorder fid Value
2 2000 9 2 0.3424053
48 2001 9 2 0.6639720
94 2002 9 2 0.5530076
140 2003 9 2 0.9757845
.....
When use 'yr' key ,report error:
df[which(yr==2001),]
Error in which(yr == 2001) : object 'yr' not found
same error by 'fid' key"
df[which(fid==2),]
Error in which(fid == 2) : object 'fid' not found
but df$yr is existed:
df$yr
[1] 2000 2000 2000 2000 2000 2000 2000 2000 2000 2000 2000 2000 2000 2000
.....
What's the problem of data frame?
r
add a comment |
up vote
0
down vote
favorite
such data frame as follows:
df <- data.frame(yr = rep(2000:2017,each=46),
dayorder = rep(seq(1,365,8),time=18),
fid = rep(1:46,time=18),
Value = runif(46*18)
)
when I sieve by keys:
df[which(dayorder==9),]
yr dayorder fid Value
2 2000 9 2 0.3424053
48 2001 9 2 0.6639720
94 2002 9 2 0.5530076
140 2003 9 2 0.9757845
.....
When use 'yr' key ,report error:
df[which(yr==2001),]
Error in which(yr == 2001) : object 'yr' not found
same error by 'fid' key"
df[which(fid==2),]
Error in which(fid == 2) : object 'fid' not found
but df$yr is existed:
df$yr
[1] 2000 2000 2000 2000 2000 2000 2000 2000 2000 2000 2000 2000 2000 2000
.....
What's the problem of data frame?
r
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
such data frame as follows:
df <- data.frame(yr = rep(2000:2017,each=46),
dayorder = rep(seq(1,365,8),time=18),
fid = rep(1:46,time=18),
Value = runif(46*18)
)
when I sieve by keys:
df[which(dayorder==9),]
yr dayorder fid Value
2 2000 9 2 0.3424053
48 2001 9 2 0.6639720
94 2002 9 2 0.5530076
140 2003 9 2 0.9757845
.....
When use 'yr' key ,report error:
df[which(yr==2001),]
Error in which(yr == 2001) : object 'yr' not found
same error by 'fid' key"
df[which(fid==2),]
Error in which(fid == 2) : object 'fid' not found
but df$yr is existed:
df$yr
[1] 2000 2000 2000 2000 2000 2000 2000 2000 2000 2000 2000 2000 2000 2000
.....
What's the problem of data frame?
r
such data frame as follows:
df <- data.frame(yr = rep(2000:2017,each=46),
dayorder = rep(seq(1,365,8),time=18),
fid = rep(1:46,time=18),
Value = runif(46*18)
)
when I sieve by keys:
df[which(dayorder==9),]
yr dayorder fid Value
2 2000 9 2 0.3424053
48 2001 9 2 0.6639720
94 2002 9 2 0.5530076
140 2003 9 2 0.9757845
.....
When use 'yr' key ,report error:
df[which(yr==2001),]
Error in which(yr == 2001) : object 'yr' not found
same error by 'fid' key"
df[which(fid==2),]
Error in which(fid == 2) : object 'fid' not found
but df$yr is existed:
df$yr
[1] 2000 2000 2000 2000 2000 2000 2000 2000 2000 2000 2000 2000 2000 2000
.....
What's the problem of data frame?
r
r
edited 22 hours ago
asked 22 hours ago
Cobin
16211
16211
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
up vote
2
down vote
accepted
You need to add df$
before the column name, in the first case you don't get an error probably because dayorder
is a variable defined in your environment.
It should've thrown an error like the others.
df[which(df$dayorder==9),] # or df[df$dayorder==9,]
When I call like this df[which(df$dayorder==9),]
I get:
Error in which(dayorder == 9) : object 'dayorder' not found
Another solution using with
, avoids too many df$
(also note you can remove which()
:
with(df, df[dayorder==9, ])
lol reason for the downvote? you people need to grow up.
– RLave
1 hour ago
add a comment |
up vote
0
down vote
you can try a tidyverse
as well
library(tidyverse)
filter(df, yr == 2001)
or base R:subset(df, yr == 2001)
– snoram
22 hours ago
Yes of course...but IMO should be included in the other answer as abaseR
solution ;)
– Jimbou
22 hours ago
add a comment |
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
2
down vote
accepted
You need to add df$
before the column name, in the first case you don't get an error probably because dayorder
is a variable defined in your environment.
It should've thrown an error like the others.
df[which(df$dayorder==9),] # or df[df$dayorder==9,]
When I call like this df[which(df$dayorder==9),]
I get:
Error in which(dayorder == 9) : object 'dayorder' not found
Another solution using with
, avoids too many df$
(also note you can remove which()
:
with(df, df[dayorder==9, ])
lol reason for the downvote? you people need to grow up.
– RLave
1 hour ago
add a comment |
up vote
2
down vote
accepted
You need to add df$
before the column name, in the first case you don't get an error probably because dayorder
is a variable defined in your environment.
It should've thrown an error like the others.
df[which(df$dayorder==9),] # or df[df$dayorder==9,]
When I call like this df[which(df$dayorder==9),]
I get:
Error in which(dayorder == 9) : object 'dayorder' not found
Another solution using with
, avoids too many df$
(also note you can remove which()
:
with(df, df[dayorder==9, ])
lol reason for the downvote? you people need to grow up.
– RLave
1 hour ago
add a comment |
up vote
2
down vote
accepted
up vote
2
down vote
accepted
You need to add df$
before the column name, in the first case you don't get an error probably because dayorder
is a variable defined in your environment.
It should've thrown an error like the others.
df[which(df$dayorder==9),] # or df[df$dayorder==9,]
When I call like this df[which(df$dayorder==9),]
I get:
Error in which(dayorder == 9) : object 'dayorder' not found
Another solution using with
, avoids too many df$
(also note you can remove which()
:
with(df, df[dayorder==9, ])
You need to add df$
before the column name, in the first case you don't get an error probably because dayorder
is a variable defined in your environment.
It should've thrown an error like the others.
df[which(df$dayorder==9),] # or df[df$dayorder==9,]
When I call like this df[which(df$dayorder==9),]
I get:
Error in which(dayorder == 9) : object 'dayorder' not found
Another solution using with
, avoids too many df$
(also note you can remove which()
:
with(df, df[dayorder==9, ])
answered 22 hours ago
RLave
2,8041820
2,8041820
lol reason for the downvote? you people need to grow up.
– RLave
1 hour ago
add a comment |
lol reason for the downvote? you people need to grow up.
– RLave
1 hour ago
lol reason for the downvote? you people need to grow up.
– RLave
1 hour ago
lol reason for the downvote? you people need to grow up.
– RLave
1 hour ago
add a comment |
up vote
0
down vote
you can try a tidyverse
as well
library(tidyverse)
filter(df, yr == 2001)
or base R:subset(df, yr == 2001)
– snoram
22 hours ago
Yes of course...but IMO should be included in the other answer as abaseR
solution ;)
– Jimbou
22 hours ago
add a comment |
up vote
0
down vote
you can try a tidyverse
as well
library(tidyverse)
filter(df, yr == 2001)
or base R:subset(df, yr == 2001)
– snoram
22 hours ago
Yes of course...but IMO should be included in the other answer as abaseR
solution ;)
– Jimbou
22 hours ago
add a comment |
up vote
0
down vote
up vote
0
down vote
you can try a tidyverse
as well
library(tidyverse)
filter(df, yr == 2001)
you can try a tidyverse
as well
library(tidyverse)
filter(df, yr == 2001)
answered 22 hours ago
Jimbou
9,44111230
9,44111230
or base R:subset(df, yr == 2001)
– snoram
22 hours ago
Yes of course...but IMO should be included in the other answer as abaseR
solution ;)
– Jimbou
22 hours ago
add a comment |
or base R:subset(df, yr == 2001)
– snoram
22 hours ago
Yes of course...but IMO should be included in the other answer as abaseR
solution ;)
– Jimbou
22 hours ago
or base R:
subset(df, yr == 2001)
– snoram
22 hours ago
or base R:
subset(df, yr == 2001)
– snoram
22 hours ago
Yes of course...but IMO should be included in the other answer as a
baseR
solution ;)– Jimbou
22 hours ago
Yes of course...but IMO should be included in the other answer as a
baseR
solution ;)– Jimbou
22 hours ago
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53372098%2fwhy-cant-subset-data-frame-by-some-keys%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown