2014年10月

as.Date returns different formats if sapply or not

I have a data frame with a date columns that I need to convert into a format R recognizes as a date.



> dataframe
Date Sum
1 06/09/15 2.51
2 06/09/15 3.75
3 06/09/15 3.50
...


I first converted it using sapply:



> dataframe$Date2<-sapply(dataframe$Date,as.Date,format="%m/%d/%y")



This returned the date as the number of days from Jan 1, 1970:



> dataframe
Date Sum Date2
1 06/09/15 2.51 16595
2 06/09/15 3.75 16595
3 06/09/15 3.50 16595
...


Later on I tried converting it without sapply:



> dataframe$Date3<-as.Date(dataframe$Date,format="%m/%m/%d")



This, in turn, returned



> dataframe
Date Sum Date2 Date3
1 06/09/15 2.51 16595 2015-09-15
2 06/09/15 3.75 16595 2015-09-15
3 06/09/15 3.50 16595 2015-09-15
...


These are two very different, apparently incompatible formats. Why does sapply return one format (days since the origin), while doing without it returns another (%Y-%m-%d)?



Now, obviously I could just ignore one method and go forth never using sapply with as.Date but I'd like to know why it reads differently. I am also struggling to convert the Date3 vector into the Date2 format.



Thus, I have two questions:




Why does sapply provide a different date format?
How do I convert a date-recognizable sequence (such as mm/dd/yyyy) into the number of days since 1 Jan 1970?

Answers

Here is an answer to the second part of your original question. To obtain the number of days since the epoch (1 Jan 1970) for a date in the format mm/dd/yyyy you can use the as.Date() function:



some.date <- as.Date("06/17/2015", "%m/%d/%Y")
days.since.epoch <- unclass(some.date)

> days.since.epoch
[1] 16616


Internally, R stores the date object some.date in terms of the number of days since the epoch (1 Jan 1970), and calling unclass() reveals this internal representation.



Answers

when working with dates I love to use lubridate as it is in my eyes much easier to use and much more intuitive then the base functions.

Your second question could be done with the following code:



require(lubridate)
dataframe$Date2<-difftime(dataframe$Date3,dmy("01-01-1970"),units="days")


depending on if you want to have the 1. January 1970 as day 1 or not you may have to add a +1 to the end of this line.



I don't really work with sapply and tapply directly (I prefer to use plyr for this) so I can't help with your first question.





Shiny output not enclosed in tags - is it possible?

I want to output a string not enclosed in tags using shiny, e.g. create <title> content. Example:



server.R



library(shiny)

shinyServer(function(input, output) {
output$mytitle <- renderText("Hello World")
})


ui.R



library(shiny)

shinyUI(fluidPage(
#tags$title(textOutput("mytitle"))
#tags$title(uiOutput("mytitle"))
tags$head(htmlOutput("mytitle", container = tags$title))
))


In every case shiny outputs things enclosed in some tags (generally <div> or <span>). Is it possible to output untagged string (text directly in <body>)? Is it possible to set <title> using shiny?



I understand that it uses jQuery to dynamically input content into HTML tags, however this behaviour possible to obtain in jQuery, so I am interested if it is so in shiny?





Array of structures in Simulink

I am trying to create an array of structures in Simulink and got some problems with it.
first of all i tried to create it directly in Simulink using this:



function a = fcn(Dibhole, t , x, const)
%#codegen
%Output = zeros(10,10);
f1 = 'number';
f2 = 'move';
cube = struct(f1, 0, f2, 0);
a = repmat(cube, 20, 10);
for i = 1:20
for j = 1:10
a(i,j).number = 0;
a(i,j).move = 0;
end
end


and i got this error:



Derived output was of type struct. 'Inherited type' is unsupported for this
type and a defined bus object must be used instead. Click on 'a' and
set data type for 'a' to be 'Bus: ', where '' is
the name of a bus object from the MATLAB workspace.



So i found some example how to create struct in Matlab and receive this to Simulink: http://blogs.mathworks.com/seth/2011/12/05/initializing-buses-using-a-matlab-structure/
That works perfectly but i still can't repeat this with array:



f1 = 'number';
f2 = 'move';
cube = struct(f1, 0, f2, 0);
myStruct2 = repmat(cube, 20, 10);
for i = 1:20
for j = 1:10
myStruct2(i,j).number = 1;
myStruct2(i,j).move = 1;
end
end

busInfo = Simulink.Bus.createObject(myStruct2);


Can anyone clarify to me what's the problem? Or maybe there is different way to create array of struct in Simulink?



Mihail



Answers

Simulink wants you to define the output of the function to be a bus.



As 'Bus: My_test_bus', for example.



Take a look at the Simulink Bus Editor. You can find it in any model under the menu, Edit->Bus Editor.



This would be a good start.



Answers

Rick, i think you are right!
i have tried this problem for a long time, and have got this results:



the irony is that I was never able to create array of structures BUT i did this with structure of arrays! :D



I made this steps for it:




to use structure of arrays we need to define and initialize it in some MATLAB function. Like this:



number = zeros(10,1);
move = zeros(10,1);

for i = 1:10

number(i,1) = i+1;
move(i,1) = i+2;
end

a = struct('numbers',number,'movement', move);

To work this data we must use Bus Selector.




So we have array in "numbers" and "movement".




BUT! Here we go, Rick: we must define type of output of MATLAB function like Bus! How to do this in simulink? i found this way:
in model properties in simulink Callbacks/PreLoadFcn define some function and in same folder as project create .m file named like this just defined function.
In this file create structure of array and define Bus type for it:



number = zeros(10,1);
move = zeros(10,1);

a = struct('numbers',number,'movement', move);

busInfo = Simulink.Bus.createObject(a);



Now we have Bus type for our structure at first loading of simulink model.




Last step: define MATLAB function output type directly.
in Model Explorer choose your MATLAB function. choose output variable. Set DataType for it: Bus:slBus1 (the name of this Bus type you can see in wokspace of matlab, because its a global variable).


That's all! now it works!



(tried to add pictures, but i have no enough reputation :( )



Now my program works in this way, but i also tried to create array of structures and still have the problems. i tried to create Bus for it, but can't transmit it to Bus Selector - it doesn't know what to do with structures... i also tried to add one more MATLAB function to create some data from structures and then display it, but it doesn't works too(





Question

what happens if your court case gets continued three or more times?



Answer

You (or your attorney) keep asking the court to dismiss it (assuming, of course, that you had nothing to do with the reason for its continuance.)



Answer

You should convince the court that the motive of your opposite party is to delay the proceedings and you will suffer an irreparable loss with delay.





Doesn't show “can't be blank”

I have field to input data of post



<%= simple_form_for @post, remote: true do |f| %>
<%= f.error_notification %>
<%= f.input :title %>
<%= f.button :submit %>
<% end %>


In model I have validation_presence_of :title, but when I confirm button without filling title it doesn't create new object. But why it just doesn't show that title can't be blank?



My new and create in controller:



 def new
@post = Post.new
end

def create
@post = Post.new(post_params)

respond_to do |format|
if @post.save
format.html { redirect_to :back, notice: 'Post was successfully created.' }
format.js
else
format.html { redirect_to :back }
format.js
end
end
end
private:
def post_params
params.require(:post).permit(:title)
end




↑このページのトップヘ