Combine filtered data from each files into new data frame
I want to filter specific line of data from multiple files and combine it into one data frame, thus that data frame should be growing each time it read new file. My code here able to loop through all the files but only create data frame of the last file. can I get my desired data frame without have to combine all-raw-bulky data from each files first?. Pls advice.
library(dplyr)
library(ggplot2)
library(tidyverse)
y = 1978
N <- 10
for (i in 1:N) {
yr = y +(as.numeric(i))
yr = as.character(yr)
p <- paste0("c:/Users/Hp/Documents/",yr,".csv")
#read.csv
dat <- read.csv(p,header = TRUE, stringsAsFactors = F)
#filter
dat_sub <- filter(dat, hght_m > 0)
dat_sub <- filter(dat_sub, temp_c > 0)
dat1_sub <- filter(dat_sub, pres_hpa == 1000)
dat2_sub <- filter(dat_sub, pres_hpa == 925)
dat3_sub <- filter(dat_sub, pres_hpa == 850)
dat4_sub <- filter(dat_sub, pres_hpa == 700)
}
all.temp <- rbind(dat1_sub,dat2_sub, dat3_sub,dat4_sub)
ggplot(data = all.temp, aes(x= as.Date(date), y = temp_c, group = pres_hpa, color = pres_hpa)) + geom_line()
r loops dataframe
add a comment |
I want to filter specific line of data from multiple files and combine it into one data frame, thus that data frame should be growing each time it read new file. My code here able to loop through all the files but only create data frame of the last file. can I get my desired data frame without have to combine all-raw-bulky data from each files first?. Pls advice.
library(dplyr)
library(ggplot2)
library(tidyverse)
y = 1978
N <- 10
for (i in 1:N) {
yr = y +(as.numeric(i))
yr = as.character(yr)
p <- paste0("c:/Users/Hp/Documents/",yr,".csv")
#read.csv
dat <- read.csv(p,header = TRUE, stringsAsFactors = F)
#filter
dat_sub <- filter(dat, hght_m > 0)
dat_sub <- filter(dat_sub, temp_c > 0)
dat1_sub <- filter(dat_sub, pres_hpa == 1000)
dat2_sub <- filter(dat_sub, pres_hpa == 925)
dat3_sub <- filter(dat_sub, pres_hpa == 850)
dat4_sub <- filter(dat_sub, pres_hpa == 700)
}
all.temp <- rbind(dat1_sub,dat2_sub, dat3_sub,dat4_sub)
ggplot(data = all.temp, aes(x= as.Date(date), y = temp_c, group = pres_hpa, color = pres_hpa)) + geom_line()
r loops dataframe
add a comment |
I want to filter specific line of data from multiple files and combine it into one data frame, thus that data frame should be growing each time it read new file. My code here able to loop through all the files but only create data frame of the last file. can I get my desired data frame without have to combine all-raw-bulky data from each files first?. Pls advice.
library(dplyr)
library(ggplot2)
library(tidyverse)
y = 1978
N <- 10
for (i in 1:N) {
yr = y +(as.numeric(i))
yr = as.character(yr)
p <- paste0("c:/Users/Hp/Documents/",yr,".csv")
#read.csv
dat <- read.csv(p,header = TRUE, stringsAsFactors = F)
#filter
dat_sub <- filter(dat, hght_m > 0)
dat_sub <- filter(dat_sub, temp_c > 0)
dat1_sub <- filter(dat_sub, pres_hpa == 1000)
dat2_sub <- filter(dat_sub, pres_hpa == 925)
dat3_sub <- filter(dat_sub, pres_hpa == 850)
dat4_sub <- filter(dat_sub, pres_hpa == 700)
}
all.temp <- rbind(dat1_sub,dat2_sub, dat3_sub,dat4_sub)
ggplot(data = all.temp, aes(x= as.Date(date), y = temp_c, group = pres_hpa, color = pres_hpa)) + geom_line()
r loops dataframe
I want to filter specific line of data from multiple files and combine it into one data frame, thus that data frame should be growing each time it read new file. My code here able to loop through all the files but only create data frame of the last file. can I get my desired data frame without have to combine all-raw-bulky data from each files first?. Pls advice.
library(dplyr)
library(ggplot2)
library(tidyverse)
y = 1978
N <- 10
for (i in 1:N) {
yr = y +(as.numeric(i))
yr = as.character(yr)
p <- paste0("c:/Users/Hp/Documents/",yr,".csv")
#read.csv
dat <- read.csv(p,header = TRUE, stringsAsFactors = F)
#filter
dat_sub <- filter(dat, hght_m > 0)
dat_sub <- filter(dat_sub, temp_c > 0)
dat1_sub <- filter(dat_sub, pres_hpa == 1000)
dat2_sub <- filter(dat_sub, pres_hpa == 925)
dat3_sub <- filter(dat_sub, pres_hpa == 850)
dat4_sub <- filter(dat_sub, pres_hpa == 700)
}
all.temp <- rbind(dat1_sub,dat2_sub, dat3_sub,dat4_sub)
ggplot(data = all.temp, aes(x= as.Date(date), y = temp_c, group = pres_hpa, color = pres_hpa)) + geom_line()
r loops dataframe
r loops dataframe
asked Nov 23 '18 at 9:52
Siti SalSiti Sal
247
247
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
You just need to put the rbind
inside the loop, like this:
library(dplyr)
library(ggplot2)
library(tidyverse)
y = 1978
N <- 10
all.temp <- NULL
for (i in 1:N) {
yr = y +(as.numeric(i))
yr = as.character(yr)
p <- paste0("c:/Users/Hp/Documents/",yr,".csv")
#read.csv
dat <- read.csv(p,header = TRUE, stringsAsFactors = F)
#filter
dat_sub <- filter(dat, hght_m > 0)
dat_sub <- filter(dat_sub, temp_c > 0)
dat1_sub <- filter(dat_sub, pres_hpa == 1000)
dat2_sub <- filter(dat_sub, pres_hpa == 925)
dat3_sub <- filter(dat_sub, pres_hpa == 850)
dat4_sub <- filter(dat_sub, pres_hpa == 700)
all.temp <- rbind(all.temp, dat1_sub, dat2_sub, dat3_sub, dat4_sub)
}
ggplot(data = all.temp, aes(x= as.Date(date), y = temp_c, group = pres_hpa, color = pres_hpa)) + geom_line()
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%2f53444280%2fcombine-filtered-data-from-each-files-into-new-data-frame%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 just need to put the rbind
inside the loop, like this:
library(dplyr)
library(ggplot2)
library(tidyverse)
y = 1978
N <- 10
all.temp <- NULL
for (i in 1:N) {
yr = y +(as.numeric(i))
yr = as.character(yr)
p <- paste0("c:/Users/Hp/Documents/",yr,".csv")
#read.csv
dat <- read.csv(p,header = TRUE, stringsAsFactors = F)
#filter
dat_sub <- filter(dat, hght_m > 0)
dat_sub <- filter(dat_sub, temp_c > 0)
dat1_sub <- filter(dat_sub, pres_hpa == 1000)
dat2_sub <- filter(dat_sub, pres_hpa == 925)
dat3_sub <- filter(dat_sub, pres_hpa == 850)
dat4_sub <- filter(dat_sub, pres_hpa == 700)
all.temp <- rbind(all.temp, dat1_sub, dat2_sub, dat3_sub, dat4_sub)
}
ggplot(data = all.temp, aes(x= as.Date(date), y = temp_c, group = pres_hpa, color = pres_hpa)) + geom_line()
add a comment |
You just need to put the rbind
inside the loop, like this:
library(dplyr)
library(ggplot2)
library(tidyverse)
y = 1978
N <- 10
all.temp <- NULL
for (i in 1:N) {
yr = y +(as.numeric(i))
yr = as.character(yr)
p <- paste0("c:/Users/Hp/Documents/",yr,".csv")
#read.csv
dat <- read.csv(p,header = TRUE, stringsAsFactors = F)
#filter
dat_sub <- filter(dat, hght_m > 0)
dat_sub <- filter(dat_sub, temp_c > 0)
dat1_sub <- filter(dat_sub, pres_hpa == 1000)
dat2_sub <- filter(dat_sub, pres_hpa == 925)
dat3_sub <- filter(dat_sub, pres_hpa == 850)
dat4_sub <- filter(dat_sub, pres_hpa == 700)
all.temp <- rbind(all.temp, dat1_sub, dat2_sub, dat3_sub, dat4_sub)
}
ggplot(data = all.temp, aes(x= as.Date(date), y = temp_c, group = pres_hpa, color = pres_hpa)) + geom_line()
add a comment |
You just need to put the rbind
inside the loop, like this:
library(dplyr)
library(ggplot2)
library(tidyverse)
y = 1978
N <- 10
all.temp <- NULL
for (i in 1:N) {
yr = y +(as.numeric(i))
yr = as.character(yr)
p <- paste0("c:/Users/Hp/Documents/",yr,".csv")
#read.csv
dat <- read.csv(p,header = TRUE, stringsAsFactors = F)
#filter
dat_sub <- filter(dat, hght_m > 0)
dat_sub <- filter(dat_sub, temp_c > 0)
dat1_sub <- filter(dat_sub, pres_hpa == 1000)
dat2_sub <- filter(dat_sub, pres_hpa == 925)
dat3_sub <- filter(dat_sub, pres_hpa == 850)
dat4_sub <- filter(dat_sub, pres_hpa == 700)
all.temp <- rbind(all.temp, dat1_sub, dat2_sub, dat3_sub, dat4_sub)
}
ggplot(data = all.temp, aes(x= as.Date(date), y = temp_c, group = pres_hpa, color = pres_hpa)) + geom_line()
You just need to put the rbind
inside the loop, like this:
library(dplyr)
library(ggplot2)
library(tidyverse)
y = 1978
N <- 10
all.temp <- NULL
for (i in 1:N) {
yr = y +(as.numeric(i))
yr = as.character(yr)
p <- paste0("c:/Users/Hp/Documents/",yr,".csv")
#read.csv
dat <- read.csv(p,header = TRUE, stringsAsFactors = F)
#filter
dat_sub <- filter(dat, hght_m > 0)
dat_sub <- filter(dat_sub, temp_c > 0)
dat1_sub <- filter(dat_sub, pres_hpa == 1000)
dat2_sub <- filter(dat_sub, pres_hpa == 925)
dat3_sub <- filter(dat_sub, pres_hpa == 850)
dat4_sub <- filter(dat_sub, pres_hpa == 700)
all.temp <- rbind(all.temp, dat1_sub, dat2_sub, dat3_sub, dat4_sub)
}
ggplot(data = all.temp, aes(x= as.Date(date), y = temp_c, group = pres_hpa, color = pres_hpa)) + geom_line()
answered Nov 23 '18 at 10:47
feebarsceviciusfeebarscevicius
47926
47926
add a comment |
add a comment |
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%2f53444280%2fcombine-filtered-data-from-each-files-into-new-data-frame%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