How to subset a df with a vector of dates and getting all the matching data?Drop factor levels in a subsetted data frameHow to join (merge) data frames (inner, outer, left, right)Use a loop to subset a vector by date rangesubset by vector in rEfficiently match elements of character vectorsApplying vectorized subsetting across multiple columns in RUsing sapply on a vector of dates: Function very slow. Why?Replacing dates in a character vector to a specific formatR subset today data with lubridate
Why does putting a dot after the URL remove login information?
How to switch an 80286 from protected to real mode?
How to check a file was encrypted (really & correctly)
Traveling from Germany to other countries by train?
Is the first page of a novel really that important?
Did silent film actors actually say their lines or did they simply improvise “dialogue” while being filmed?
Where to pee in London?
Purchased new computer from DELL with pre-installed Ubuntu. Won't boot. Should assume its an error from DELL?
If the interviewer says "We have other interviews to conduct and then back to you in few days", is it a bad sign to not get the job?
Ubuntu show wrong disk sizes, how to solve it?
Does the length of a password for Wi-Fi affect speed?
How many years before enough atoms of your body are replaced to survive the sudden disappearance of the original body’s atoms?
Based on what criteria do you add/not add icons to labels within a toolbar?
If someone else uploads my GPL'd code to Github without my permission, is that a copyright violation?
How easy is it to get a gun illegally in the United States?
Is DC heating faster than AC heating?
split large formula in align
Did Apollo leave poop on the moon?
A verb for when some rights are not violated?
Why do proponents of guns oppose gun competency tests?
Do some languages mention the top limit of a range first?
Can chords be inferred from melody alone?
…down the primrose path
What are the function of EM and EN spaces?
How to subset a df with a vector of dates and getting all the matching data?
Drop factor levels in a subsetted data frameHow to join (merge) data frames (inner, outer, left, right)Use a loop to subset a vector by date rangesubset by vector in rEfficiently match elements of character vectorsApplying vectorized subsetting across multiple columns in RUsing sapply on a vector of dates: Function very slow. Why?Replacing dates in a character vector to a specific formatR subset today data with lubridate
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;
I am trying to subset my df bases on matching dates. I have the dates i require with the date format. The date on my df is on the same format.
When I try my code I am getting only the first row matching the subset and there could be 20 matching rows per date.
I tried using subset with a logical search in the df matching the vector
subset.met=subset(hr.met, hr.met$date == dates)
This is my df
1 2018-12-20 07:00:00 NA 984.3140 4.7425 4.240365 203.2252
2 2018-12-20 08:00:00 NA 986.6738 2.6975 2.036287 175.7522
3 2018-12-20 09:00:00 NA 988.1152 3.7025 3.411026 212.9876
4 2018-12-20 10:00:00 NA 989.2403 3.7700 3.297027 216.7082
5 2018-12-20 11:00:00 NA 990.4552 4.0250 3.511919 225.3250
6 2018-12-20 12:00:00 NA 990.5790 3.7175 3.317329 222.5587
dwd t2 rh sat vp h2o date
1 24.77761 NA NA NA NA NA 2018-12-20
2 41.94633 NA NA NA NA NA 2018-12-20
3 21.79864 NA NA NA NA NA 2018-12-20
4 28.37998 NA NA NA NA NA 2018-12-20
5 27.86019 NA NA NA NA NA 2018-12-20
6 25.02467 NA NA NA NA NA 2018-12-20
This is my dates vector
"2019-02-06" "2019-02-07" "2019-02-08" "2019-02-11" "2019-02-12" "2019-02-13" "2019-02-15" "2019-02-16" "2019-02-17" "2019-02-18"
"2019-02-20" "2019-02-21" "2019-02-26" "2019-02-27" "2019-02-28" "2019-03-01" "2019-03-02" "2019-03-03" "2019-03-04" "2019-03-05"
"2019-03-12" "2019-03-13" "2019-03-14" "2019-03-15" "2019-03-16" "2019-03-17" "2019-03-19"
This is what I am getting
1004 2019-01-31 02:00:00 NA 990.8791 2.880833 2.716300
1042 2019-02-01 16:00:00 NA 1001.9768 1.759167 1.652109
1081 2019-02-03 07:00:00 NA 994.9080 3.465000 3.127165
1119 2019-02-04 21:00:00 NA 997.8129 3.465000 3.115460
1158 2019-02-06 12:00:00 296.4433 999.6472 2.322500 2.196016
1197 2019-02-08 03:00:00 306.4103 987.3516 2.096667 1.946248
wd dwd t2 rh sat vp
1004 16.40826 18.24759 NA NA NA NA
1042 213.26311 17.81623 NA NA NA NA
1081 29.44621 24.45101 NA NA NA NA
1119 203.37526 24.97175 NA NA NA NA
1158 25.21708 17.95901 297.0304 82.88533 29.61701 24.54474
1197 27.73385 20.24611 306.8247 39.54208 52.31900 20.19168
h2o date
1004 NA 2019-01-31
1042 NA 2019-02-01
1081 NA 2019-02-03
1119 NA 2019-02-04
1158 993.8726 2019-02-06
1197 791.6034 2019-02-08
If you can see I am getting only the first matching row of every date, and I need all the rows.
r
add a comment |
I am trying to subset my df bases on matching dates. I have the dates i require with the date format. The date on my df is on the same format.
When I try my code I am getting only the first row matching the subset and there could be 20 matching rows per date.
I tried using subset with a logical search in the df matching the vector
subset.met=subset(hr.met, hr.met$date == dates)
This is my df
1 2018-12-20 07:00:00 NA 984.3140 4.7425 4.240365 203.2252
2 2018-12-20 08:00:00 NA 986.6738 2.6975 2.036287 175.7522
3 2018-12-20 09:00:00 NA 988.1152 3.7025 3.411026 212.9876
4 2018-12-20 10:00:00 NA 989.2403 3.7700 3.297027 216.7082
5 2018-12-20 11:00:00 NA 990.4552 4.0250 3.511919 225.3250
6 2018-12-20 12:00:00 NA 990.5790 3.7175 3.317329 222.5587
dwd t2 rh sat vp h2o date
1 24.77761 NA NA NA NA NA 2018-12-20
2 41.94633 NA NA NA NA NA 2018-12-20
3 21.79864 NA NA NA NA NA 2018-12-20
4 28.37998 NA NA NA NA NA 2018-12-20
5 27.86019 NA NA NA NA NA 2018-12-20
6 25.02467 NA NA NA NA NA 2018-12-20
This is my dates vector
"2019-02-06" "2019-02-07" "2019-02-08" "2019-02-11" "2019-02-12" "2019-02-13" "2019-02-15" "2019-02-16" "2019-02-17" "2019-02-18"
"2019-02-20" "2019-02-21" "2019-02-26" "2019-02-27" "2019-02-28" "2019-03-01" "2019-03-02" "2019-03-03" "2019-03-04" "2019-03-05"
"2019-03-12" "2019-03-13" "2019-03-14" "2019-03-15" "2019-03-16" "2019-03-17" "2019-03-19"
This is what I am getting
1004 2019-01-31 02:00:00 NA 990.8791 2.880833 2.716300
1042 2019-02-01 16:00:00 NA 1001.9768 1.759167 1.652109
1081 2019-02-03 07:00:00 NA 994.9080 3.465000 3.127165
1119 2019-02-04 21:00:00 NA 997.8129 3.465000 3.115460
1158 2019-02-06 12:00:00 296.4433 999.6472 2.322500 2.196016
1197 2019-02-08 03:00:00 306.4103 987.3516 2.096667 1.946248
wd dwd t2 rh sat vp
1004 16.40826 18.24759 NA NA NA NA
1042 213.26311 17.81623 NA NA NA NA
1081 29.44621 24.45101 NA NA NA NA
1119 203.37526 24.97175 NA NA NA NA
1158 25.21708 17.95901 297.0304 82.88533 29.61701 24.54474
1197 27.73385 20.24611 306.8247 39.54208 52.31900 20.19168
h2o date
1004 NA 2019-01-31
1042 NA 2019-02-01
1081 NA 2019-02-03
1119 NA 2019-02-04
1158 993.8726 2019-02-06
1197 791.6034 2019-02-08
If you can see I am getting only the first matching row of every date, and I need all the rows.
r
use%in%
,subset.met=subset(hr.met, date %in% dates)
– Ronak Shah
Mar 27 at 4:05
add a comment |
I am trying to subset my df bases on matching dates. I have the dates i require with the date format. The date on my df is on the same format.
When I try my code I am getting only the first row matching the subset and there could be 20 matching rows per date.
I tried using subset with a logical search in the df matching the vector
subset.met=subset(hr.met, hr.met$date == dates)
This is my df
1 2018-12-20 07:00:00 NA 984.3140 4.7425 4.240365 203.2252
2 2018-12-20 08:00:00 NA 986.6738 2.6975 2.036287 175.7522
3 2018-12-20 09:00:00 NA 988.1152 3.7025 3.411026 212.9876
4 2018-12-20 10:00:00 NA 989.2403 3.7700 3.297027 216.7082
5 2018-12-20 11:00:00 NA 990.4552 4.0250 3.511919 225.3250
6 2018-12-20 12:00:00 NA 990.5790 3.7175 3.317329 222.5587
dwd t2 rh sat vp h2o date
1 24.77761 NA NA NA NA NA 2018-12-20
2 41.94633 NA NA NA NA NA 2018-12-20
3 21.79864 NA NA NA NA NA 2018-12-20
4 28.37998 NA NA NA NA NA 2018-12-20
5 27.86019 NA NA NA NA NA 2018-12-20
6 25.02467 NA NA NA NA NA 2018-12-20
This is my dates vector
"2019-02-06" "2019-02-07" "2019-02-08" "2019-02-11" "2019-02-12" "2019-02-13" "2019-02-15" "2019-02-16" "2019-02-17" "2019-02-18"
"2019-02-20" "2019-02-21" "2019-02-26" "2019-02-27" "2019-02-28" "2019-03-01" "2019-03-02" "2019-03-03" "2019-03-04" "2019-03-05"
"2019-03-12" "2019-03-13" "2019-03-14" "2019-03-15" "2019-03-16" "2019-03-17" "2019-03-19"
This is what I am getting
1004 2019-01-31 02:00:00 NA 990.8791 2.880833 2.716300
1042 2019-02-01 16:00:00 NA 1001.9768 1.759167 1.652109
1081 2019-02-03 07:00:00 NA 994.9080 3.465000 3.127165
1119 2019-02-04 21:00:00 NA 997.8129 3.465000 3.115460
1158 2019-02-06 12:00:00 296.4433 999.6472 2.322500 2.196016
1197 2019-02-08 03:00:00 306.4103 987.3516 2.096667 1.946248
wd dwd t2 rh sat vp
1004 16.40826 18.24759 NA NA NA NA
1042 213.26311 17.81623 NA NA NA NA
1081 29.44621 24.45101 NA NA NA NA
1119 203.37526 24.97175 NA NA NA NA
1158 25.21708 17.95901 297.0304 82.88533 29.61701 24.54474
1197 27.73385 20.24611 306.8247 39.54208 52.31900 20.19168
h2o date
1004 NA 2019-01-31
1042 NA 2019-02-01
1081 NA 2019-02-03
1119 NA 2019-02-04
1158 993.8726 2019-02-06
1197 791.6034 2019-02-08
If you can see I am getting only the first matching row of every date, and I need all the rows.
r
I am trying to subset my df bases on matching dates. I have the dates i require with the date format. The date on my df is on the same format.
When I try my code I am getting only the first row matching the subset and there could be 20 matching rows per date.
I tried using subset with a logical search in the df matching the vector
subset.met=subset(hr.met, hr.met$date == dates)
This is my df
1 2018-12-20 07:00:00 NA 984.3140 4.7425 4.240365 203.2252
2 2018-12-20 08:00:00 NA 986.6738 2.6975 2.036287 175.7522
3 2018-12-20 09:00:00 NA 988.1152 3.7025 3.411026 212.9876
4 2018-12-20 10:00:00 NA 989.2403 3.7700 3.297027 216.7082
5 2018-12-20 11:00:00 NA 990.4552 4.0250 3.511919 225.3250
6 2018-12-20 12:00:00 NA 990.5790 3.7175 3.317329 222.5587
dwd t2 rh sat vp h2o date
1 24.77761 NA NA NA NA NA 2018-12-20
2 41.94633 NA NA NA NA NA 2018-12-20
3 21.79864 NA NA NA NA NA 2018-12-20
4 28.37998 NA NA NA NA NA 2018-12-20
5 27.86019 NA NA NA NA NA 2018-12-20
6 25.02467 NA NA NA NA NA 2018-12-20
This is my dates vector
"2019-02-06" "2019-02-07" "2019-02-08" "2019-02-11" "2019-02-12" "2019-02-13" "2019-02-15" "2019-02-16" "2019-02-17" "2019-02-18"
"2019-02-20" "2019-02-21" "2019-02-26" "2019-02-27" "2019-02-28" "2019-03-01" "2019-03-02" "2019-03-03" "2019-03-04" "2019-03-05"
"2019-03-12" "2019-03-13" "2019-03-14" "2019-03-15" "2019-03-16" "2019-03-17" "2019-03-19"
This is what I am getting
1004 2019-01-31 02:00:00 NA 990.8791 2.880833 2.716300
1042 2019-02-01 16:00:00 NA 1001.9768 1.759167 1.652109
1081 2019-02-03 07:00:00 NA 994.9080 3.465000 3.127165
1119 2019-02-04 21:00:00 NA 997.8129 3.465000 3.115460
1158 2019-02-06 12:00:00 296.4433 999.6472 2.322500 2.196016
1197 2019-02-08 03:00:00 306.4103 987.3516 2.096667 1.946248
wd dwd t2 rh sat vp
1004 16.40826 18.24759 NA NA NA NA
1042 213.26311 17.81623 NA NA NA NA
1081 29.44621 24.45101 NA NA NA NA
1119 203.37526 24.97175 NA NA NA NA
1158 25.21708 17.95901 297.0304 82.88533 29.61701 24.54474
1197 27.73385 20.24611 306.8247 39.54208 52.31900 20.19168
h2o date
1004 NA 2019-01-31
1042 NA 2019-02-01
1081 NA 2019-02-03
1119 NA 2019-02-04
1158 993.8726 2019-02-06
1197 791.6034 2019-02-08
If you can see I am getting only the first matching row of every date, and I need all the rows.
r
r
edited Mar 27 at 5:22
RAB
2,0105 silver badges22 bronze badges
2,0105 silver badges22 bronze badges
asked Mar 27 at 4:04
JhonathanJhonathan
103 bronze badges
103 bronze badges
use%in%
,subset.met=subset(hr.met, date %in% dates)
– Ronak Shah
Mar 27 at 4:05
add a comment |
use%in%
,subset.met=subset(hr.met, date %in% dates)
– Ronak Shah
Mar 27 at 4:05
use
%in%
, subset.met=subset(hr.met, date %in% dates)
– Ronak Shah
Mar 27 at 4:05
use
%in%
, subset.met=subset(hr.met, date %in% dates)
– Ronak Shah
Mar 27 at 4:05
add a comment |
1 Answer
1
active
oldest
votes
You can create a logical vector which checks if the dates
are present as below:
hr.met$date %in% dates
grepl(dates, hr.met$date)
And then subset using that vector like:
subset(hr.met, date %in% dates)
dplyr::filter(hr.met, date %in% dates)
hr.met[grepl(dates, hr.met$date), ]
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
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%2f55369628%2fhow-to-subset-a-df-with-a-vector-of-dates-and-getting-all-the-matching-data%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
You can create a logical vector which checks if the dates
are present as below:
hr.met$date %in% dates
grepl(dates, hr.met$date)
And then subset using that vector like:
subset(hr.met, date %in% dates)
dplyr::filter(hr.met, date %in% dates)
hr.met[grepl(dates, hr.met$date), ]
add a comment |
You can create a logical vector which checks if the dates
are present as below:
hr.met$date %in% dates
grepl(dates, hr.met$date)
And then subset using that vector like:
subset(hr.met, date %in% dates)
dplyr::filter(hr.met, date %in% dates)
hr.met[grepl(dates, hr.met$date), ]
add a comment |
You can create a logical vector which checks if the dates
are present as below:
hr.met$date %in% dates
grepl(dates, hr.met$date)
And then subset using that vector like:
subset(hr.met, date %in% dates)
dplyr::filter(hr.met, date %in% dates)
hr.met[grepl(dates, hr.met$date), ]
You can create a logical vector which checks if the dates
are present as below:
hr.met$date %in% dates
grepl(dates, hr.met$date)
And then subset using that vector like:
subset(hr.met, date %in% dates)
dplyr::filter(hr.met, date %in% dates)
hr.met[grepl(dates, hr.met$date), ]
answered Mar 27 at 5:17
SonnySonny
2,7051 gold badge6 silver badges17 bronze badges
2,7051 gold badge6 silver badges17 bronze badges
add a comment |
add a comment |
Got a question that you can’t ask on public Stack Overflow? Learn more about sharing private information with Stack Overflow for Teams.
Got a question that you can’t ask on public Stack Overflow? Learn more about sharing private information with Stack Overflow for Teams.
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
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%2f55369628%2fhow-to-subset-a-df-with-a-vector-of-dates-and-getting-all-the-matching-data%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
use
%in%
,subset.met=subset(hr.met, date %in% dates)
– Ronak Shah
Mar 27 at 4:05