Problem with date an time program in a a dataframe
I have been asked for my internship to write a function that runs through a dataframe with date and time and gives back some cells of the rows that are 20h before.
Attached is a picture of part of the dataframe; ID is the number of the animal, Date is when the animal was seen, Time is the time when the animal was seen (ex:2 stands for 2hour), Lat and Long are the coordinates of the animal.
Then, the function takes into input (ID, Date+time) and returns the coordinates of where the animal was 20 hours before (columns 4 and 5 of the data frame).
The error message is always the same:
Error in print(X) : object 'X' not found
It seems like the conditions in the IF clause are never reunited...
Here is the script:
data <- data.frame(GPS_data_for_R) #data contains the excel spreadsheet
data$NewTime <- ymd_h(paste(data$Date,as.character(data$Time))) #a new column is created that merge the Date and the Time columns into a POSIXct format
# This function returns the coordinates of the animal 20h before, depending on the ID, Dtae and Time the user wants
Feeding_coordinates <- function (ID, NewTime){
for ( i in (1:length(data) ) ) {
if ( data[i,1] == ID & data[i,6] == as.POSIXct(NewTime,format="%Y-%m-%d %H:%M:%OS", tz="UTC")-72000){ #if the ID of the animal matches the request and if the NewTime - 20h also
data[i,4] <- X #then X takes the value of the Longitude
data[i,5] <- Y }# and Y the value of the Lagitude
}
print (X)
print (Y)
}
I really have no clue why it doesn't work, so any help is very welcomed, I have already spent so much time on it!
Thank you,
Manon.
r date dataframe time posix
add a comment |
I have been asked for my internship to write a function that runs through a dataframe with date and time and gives back some cells of the rows that are 20h before.
Attached is a picture of part of the dataframe; ID is the number of the animal, Date is when the animal was seen, Time is the time when the animal was seen (ex:2 stands for 2hour), Lat and Long are the coordinates of the animal.
Then, the function takes into input (ID, Date+time) and returns the coordinates of where the animal was 20 hours before (columns 4 and 5 of the data frame).
The error message is always the same:
Error in print(X) : object 'X' not found
It seems like the conditions in the IF clause are never reunited...
Here is the script:
data <- data.frame(GPS_data_for_R) #data contains the excel spreadsheet
data$NewTime <- ymd_h(paste(data$Date,as.character(data$Time))) #a new column is created that merge the Date and the Time columns into a POSIXct format
# This function returns the coordinates of the animal 20h before, depending on the ID, Dtae and Time the user wants
Feeding_coordinates <- function (ID, NewTime){
for ( i in (1:length(data) ) ) {
if ( data[i,1] == ID & data[i,6] == as.POSIXct(NewTime,format="%Y-%m-%d %H:%M:%OS", tz="UTC")-72000){ #if the ID of the animal matches the request and if the NewTime - 20h also
data[i,4] <- X #then X takes the value of the Longitude
data[i,5] <- Y }# and Y the value of the Lagitude
}
print (X)
print (Y)
}
I really have no clue why it doesn't work, so any help is very welcomed, I have already spent so much time on it!
Thank you,
Manon.
r date dataframe time posix
Welcome to SO. Pictures are neither code nor data unless the topic is image processing. Please click on the "r" below your question, then click on the "info" tab and read up on the tips provided there (some via links) on how to craft a good question that includes real data, all R packages being used and complete (but minimal) reproducible code to help others help you.
– hrbrmstr
Nov 26 '18 at 13:49
add a comment |
I have been asked for my internship to write a function that runs through a dataframe with date and time and gives back some cells of the rows that are 20h before.
Attached is a picture of part of the dataframe; ID is the number of the animal, Date is when the animal was seen, Time is the time when the animal was seen (ex:2 stands for 2hour), Lat and Long are the coordinates of the animal.
Then, the function takes into input (ID, Date+time) and returns the coordinates of where the animal was 20 hours before (columns 4 and 5 of the data frame).
The error message is always the same:
Error in print(X) : object 'X' not found
It seems like the conditions in the IF clause are never reunited...
Here is the script:
data <- data.frame(GPS_data_for_R) #data contains the excel spreadsheet
data$NewTime <- ymd_h(paste(data$Date,as.character(data$Time))) #a new column is created that merge the Date and the Time columns into a POSIXct format
# This function returns the coordinates of the animal 20h before, depending on the ID, Dtae and Time the user wants
Feeding_coordinates <- function (ID, NewTime){
for ( i in (1:length(data) ) ) {
if ( data[i,1] == ID & data[i,6] == as.POSIXct(NewTime,format="%Y-%m-%d %H:%M:%OS", tz="UTC")-72000){ #if the ID of the animal matches the request and if the NewTime - 20h also
data[i,4] <- X #then X takes the value of the Longitude
data[i,5] <- Y }# and Y the value of the Lagitude
}
print (X)
print (Y)
}
I really have no clue why it doesn't work, so any help is very welcomed, I have already spent so much time on it!
Thank you,
Manon.
r date dataframe time posix
I have been asked for my internship to write a function that runs through a dataframe with date and time and gives back some cells of the rows that are 20h before.
Attached is a picture of part of the dataframe; ID is the number of the animal, Date is when the animal was seen, Time is the time when the animal was seen (ex:2 stands for 2hour), Lat and Long are the coordinates of the animal.
Then, the function takes into input (ID, Date+time) and returns the coordinates of where the animal was 20 hours before (columns 4 and 5 of the data frame).
The error message is always the same:
Error in print(X) : object 'X' not found
It seems like the conditions in the IF clause are never reunited...
Here is the script:
data <- data.frame(GPS_data_for_R) #data contains the excel spreadsheet
data$NewTime <- ymd_h(paste(data$Date,as.character(data$Time))) #a new column is created that merge the Date and the Time columns into a POSIXct format
# This function returns the coordinates of the animal 20h before, depending on the ID, Dtae and Time the user wants
Feeding_coordinates <- function (ID, NewTime){
for ( i in (1:length(data) ) ) {
if ( data[i,1] == ID & data[i,6] == as.POSIXct(NewTime,format="%Y-%m-%d %H:%M:%OS", tz="UTC")-72000){ #if the ID of the animal matches the request and if the NewTime - 20h also
data[i,4] <- X #then X takes the value of the Longitude
data[i,5] <- Y }# and Y the value of the Lagitude
}
print (X)
print (Y)
}
I really have no clue why it doesn't work, so any help is very welcomed, I have already spent so much time on it!
Thank you,
Manon.
r date dataframe time posix
r date dataframe time posix
edited Nov 26 '18 at 13:48
Yannick
751615
751615
asked Nov 26 '18 at 13:08
Manon MorelManon Morel
1
1
Welcome to SO. Pictures are neither code nor data unless the topic is image processing. Please click on the "r" below your question, then click on the "info" tab and read up on the tips provided there (some via links) on how to craft a good question that includes real data, all R packages being used and complete (but minimal) reproducible code to help others help you.
– hrbrmstr
Nov 26 '18 at 13:49
add a comment |
Welcome to SO. Pictures are neither code nor data unless the topic is image processing. Please click on the "r" below your question, then click on the "info" tab and read up on the tips provided there (some via links) on how to craft a good question that includes real data, all R packages being used and complete (but minimal) reproducible code to help others help you.
– hrbrmstr
Nov 26 '18 at 13:49
Welcome to SO. Pictures are neither code nor data unless the topic is image processing. Please click on the "r" below your question, then click on the "info" tab and read up on the tips provided there (some via links) on how to craft a good question that includes real data, all R packages being used and complete (but minimal) reproducible code to help others help you.
– hrbrmstr
Nov 26 '18 at 13:49
Welcome to SO. Pictures are neither code nor data unless the topic is image processing. Please click on the "r" below your question, then click on the "info" tab and read up on the tips provided there (some via links) on how to craft a good question that includes real data, all R packages being used and complete (but minimal) reproducible code to help others help you.
– hrbrmstr
Nov 26 '18 at 13:49
add a comment |
1 Answer
1
active
oldest
votes
Since no (sample) data is provided (yet), it is hard to help you out. However, if your data is structured so that each row resembles one hour, and you want to get the row from 20 hours before, you might want to use dplyr
's lag()
.
library(dplyr)
data %>%
group_by(ID) %>% # Perhaps optional
mutate(feed_coord_lat=lag(Lat, 20),
feef_coord_long=lag(Long, 20))
This gives you the whole dataframe with two lagged coordinates columns. You can also build a function out of this.
add a comment |
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%2f53481822%2fproblem-with-date-an-time-program-in-a-a-dataframe%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
Since no (sample) data is provided (yet), it is hard to help you out. However, if your data is structured so that each row resembles one hour, and you want to get the row from 20 hours before, you might want to use dplyr
's lag()
.
library(dplyr)
data %>%
group_by(ID) %>% # Perhaps optional
mutate(feed_coord_lat=lag(Lat, 20),
feef_coord_long=lag(Long, 20))
This gives you the whole dataframe with two lagged coordinates columns. You can also build a function out of this.
add a comment |
Since no (sample) data is provided (yet), it is hard to help you out. However, if your data is structured so that each row resembles one hour, and you want to get the row from 20 hours before, you might want to use dplyr
's lag()
.
library(dplyr)
data %>%
group_by(ID) %>% # Perhaps optional
mutate(feed_coord_lat=lag(Lat, 20),
feef_coord_long=lag(Long, 20))
This gives you the whole dataframe with two lagged coordinates columns. You can also build a function out of this.
add a comment |
Since no (sample) data is provided (yet), it is hard to help you out. However, if your data is structured so that each row resembles one hour, and you want to get the row from 20 hours before, you might want to use dplyr
's lag()
.
library(dplyr)
data %>%
group_by(ID) %>% # Perhaps optional
mutate(feed_coord_lat=lag(Lat, 20),
feef_coord_long=lag(Long, 20))
This gives you the whole dataframe with two lagged coordinates columns. You can also build a function out of this.
Since no (sample) data is provided (yet), it is hard to help you out. However, if your data is structured so that each row resembles one hour, and you want to get the row from 20 hours before, you might want to use dplyr
's lag()
.
library(dplyr)
data %>%
group_by(ID) %>% # Perhaps optional
mutate(feed_coord_lat=lag(Lat, 20),
feef_coord_long=lag(Long, 20))
This gives you the whole dataframe with two lagged coordinates columns. You can also build a function out of this.
answered Nov 26 '18 at 14:14
Ben T.Ben T.
3117
3117
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%2f53481822%2fproblem-with-date-an-time-program-in-a-a-dataframe%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
Welcome to SO. Pictures are neither code nor data unless the topic is image processing. Please click on the "r" below your question, then click on the "info" tab and read up on the tips provided there (some via links) on how to craft a good question that includes real data, all R packages being used and complete (but minimal) reproducible code to help others help you.
– hrbrmstr
Nov 26 '18 at 13:49