How to specify the date format of column in Rscript - r

I have tried this. But its giving me error
Datecreated<-c('Created Time')
I will get this data from cloud using APIs. I need to define the format of this Created Time column as d-mon-yy
For example- 18-Nov-17
How can I achiEve this. I am new to R.
Any help woud be appreciated.

Your screenshot is showing you assign the string ‘created time’ to the variable Datecreated. I’m not sure where or how you’re getting your data but I’m assuming that is your issue. Once you have your data you can use akrun’s answer of as.Date(column, “%d-%b-%y”)
MyDates <- c(“17-Jan-16”, “6-Feb-17”)
FormattedDates <- as.Date(MyDates, “%d-%b-%y”)

Related

How do you download data from API and export it into a nice CSV file to query?

I am trying to figure out how to 'download' data into a nice CSV file to be able to analyse.
I am currently looking at WHO data here:
I am doing so through following documentation and getting output like so:
test_data <- jsonlite::parse_json(url("http://apps.who.int/gho/athena/api/GHO/WHS6_102.json?profile=simple"))
head(test_data)
This gives me a rather messy list of list of lists.
For example:
I get this
It is not very easy to analyse and rather messy. How could I clean this up by using say two columns that is returned from this json_parse, information only from say dim like REGION, YEAR, COUNTRY and then the values from the column Value. I would like to make this into a nice dataframe/CSV file so I can then more easily understand what is happening.
Can anyone give any advice?
jsonlite::fromJSON gives you data in a better format and the 3rd element in the list is where the main data is.
url <- 'https://apps.who.int/gho/athena/api/GHO/WHS6_102.json?profile=simple'
tmp <- jsonlite::fromJSON(url)
data <- tmp[[3]]

Cannot get head of sparkR data frame

I use sparkR to run some sql query to get sparkR data frame like the following
data = sql(sql_query)
And I can get the dimension of data by using dim(data)
However, when I want to take a look at the data by using head(data) it will fail and give me an error java.lang
.ClassCastException: org.apache.hadoop.hive.serde2.io.TimestampWritable cannot be
cast to org.apache.hadoop.io.IntWritable
I tried the sql query in Hive and it doesn't have any problem. The weird thing here is I can get the dimension but cannot get the head.
Any idea?
Try: View(head(data, 20))...also, it would be helpful if you could give a little bit more detail on your question.

Why am I getting different output from the Alteryx R tool

I using the Alteryx R Tool to sign an amazon http request. To do so, I need the hmac function that is included in the digest package.
I'm using a text input tool that includes the key and a datestamp.
Key= "foo"
datastamp= "20120215"
Here's the issue. When I run the following script:
the.data <- read.Alteryx("1", mode="data.frame")
write.Alteryx(base64encode(hmac(the.data$key,the.data$datestamp,algo="sha256",raw = TRUE)),1)
I get an incorrect result when compared to when I run the following:
write.Alteryx(base64encode(hmac("foo","20120215",algo="sha256",raw = TRUE)),1)
The difference being when I hardcode the values for the key and object I get the correct result. But if use the variables from the R data frame I get incorrect output.
Does the data frame alter the data in someway. Has anyone come across this when working with the R Tool in Alteryx.
Thanks for your input.
The issue appears to be that when creating the data frame, your character variables are converted to factors. The way to fix this with the data.frame constructor function is
the.data <- data.frame(Key="foo", datestamp="20120215", stringsAsFactors=FALSE)
I haven't used read.Alteryx but I assume it has a similar way of achieving this.
Alternatively, if your data frame has already been created, you can convert the factors back into character:
write.Alteryx(base64encode(hmac(
as.character(the.data$Key),
as.character(the.data$datestamp),
algo="sha256",raw = TRUE)),1)

exporting plotted variable shows blank image

I am doing java and R integration using JRI.
Please find below script
String path = "C:\\Users\\hrpatel\\Desktop\\CSVs\\DataNVOCT.csv";
rengine.eval("library(tseries)");
rengine.eval(String.format("mydata <- read.csv('%s')",path.replace('\\', '/')));
String exportFilePath= "C:\\Users\\hrpatel\\Desktop\\CSVs\\arima3.jpg";
rengine.eval("Y <- NewVisits");
rengine.eval("t <- Day.Index");
rengine.eval("summary(Y)");
rengine.eval("adf.test(Y, alternative='stationary')");
rengine.eval("adf.test(Y, alternative='stationary', k=0)");
rengine.eval("acf(Y)");
rengine.eval("pacf(Y)");
rengine.eval("mydata.arima101 <- arima(Y,order=c(1,0,1))");
rengine.eval("mydata.pred1 <- predict(mydata.arima101, n.ahead=1000)");
rengine.eval(String.format("jpeg('%s')",exportFilePath.replace('\\', '/')));
rengine.eval("plot(t,Y)");
rengine.eval("lines(mydata.pred1$pred, col='blue',size=10)");
rengine.eval("lines(mydata.pred1$pred+1*mydata.pred1$se, col='red')");
rengine.eval("lines(mydata.pred1$pred-1*mydata.pred1$se, col='red')");
rengine.eval("dev.off()");
In above codebase when i tried plot(t,Y) or plot(Y). it export a blank image, while in case of plot(mydata) it is working file.
One more thing when i run above code in R it creates the image(using JRI it shows blank image).
I have spend 1 day to solve this but i dont found any solution.
Please suggest if you have any alternatives.
Your help is needed.
Thanks in Advance
if i understand correctly, you have a data set named mydata, that has two columns, NewVisits, and Day.Index, in that case you need to change:
rengine.eval("Y <- NewVisits");
to
rengine.eval("Y <- mydata$NewVisits");
and
rengine.eval("t <- Day.Index");
to
rengine.eval("t <- mydata$Day.Index");
This also explains why plot(mydata) works for you - because R recognizes it.
if this isn't the solution, then i cant see where you are reading NewVisits and Day.Index from
BTW i stongly recommend to plot using the ggplot package

R shiny modify reactive data frame

I'm trying to build an app in which I can select the data file (input$dataset), then add a new datetime column formatting date and time previous columns to make plots with ggplot2.
I use 'within' that previously worked in batch scripts and in Rstudio. But now I get this error message:
no applicable method for 'within' applied to an object of class
"reactive"
How can I apply this method to a reactive object? Should I use another command? cbind? ddply?
datos=reactive({
read.csv(input$dataset,header=T,sep=";",na.strings="-99.900")
within(datos, datetime <- as.POSIXct(paste(FECHA,H_SOLAR),format = "%y/%m/%d %H:%M:%S"))
})
Thanks in advance
EDIT:
Following the answer below I understand a reactive source can't be modified, say add a column to the data frame. The point is I want to use ggplot in this way (adapting an old R script):
p=ggplot(datos(),aes_string(x="datetime", y=input$var,colour="as.character(stat_id)")) +
geom_line()
}
So, how should I add datetime to datos? Maybe creating datos2 as a new reactive source merging datos and datetime?
EDIT 2
Added full code to github https://github.com/pacomet/git
You cannot change the datafile directly - it is a reactive source that cannot be changed except by a user input (in this case the choice of data file).
You have 2 choices (that I know of):
1) Make a new object that holds the reformatted date:
NewDate<-reactive({ as.POSIXct(paste(FECHA,H_SOLAR),
format = "%y/%m/%d %H:%M:%S")})
then use NewDate() as your variable for graphing.
2) Change the date format within the function that makes the graph. e.g.
plot(x~as.POSIXct(paste(FECHA,H_SOLAR),format="%y/%m/%d %H:%M:%S"),
data=datos())
Here is a somewhat similar issue:Formatting reactive data.frame
EDIT
In response to the edited question - here is an updated answer.
I don't know much about ggplot but if the issue is to get this all into one data.frame, then you might want to do something like this:
datos=reactive({read.csv(input$dataset,header=T,sep=";",na.strings="-99.900"))}
NewDate<-reactive <- ({as.POSIXct(paste(FECHA,H_SOLAR),
format = "%y/%m/%d %H:%M:%S" )})
datos2<-reactive({ data.frame(datos(),NewDate() })
Then try using datos2() in ggplot - I think that should give you what you need.
I think this question can be closed thanks to the answer for #dieter-menne to another question about subsetting reactive data frames. The point is to create a new local variable, similar to #john-paul suggestion.
Please take a look at https://stackoverflow.com/a/20569512/709777

Resources