Currently I have the timestamp column in time format with values such as :
1041592
1040583
1048448
and when I am applying the datetime18. format on this column I am getting the following values
27OCT78:16:21:21
15SEP78:16:21:21
09AUG79:08:58:39
Could you please help me where I am going wrong , I want it to be displayed in date and time format , CURRENTLY iam getting the above values.
You need show your work. I don't get what you say you get.
122 data _null_;
123 input dt ##;
124 put 'NOTE: ' (3*dt) (=best. =datetime. =time12.);
125 cards;
NOTE: dt=1041592 dt=13JAN60:01:19:52 dt=289:19:52
NOTE: dt=1040583 dt=13JAN60:01:03:03 dt=289:03:03
NOTE: dt=1048448 dt=13JAN60:03:14:08 dt=291:14:08
Related
Using PHP and MariaDB my knowledge is fairly good. My knowledge in running 100% MariaDB is stack overflow and Google and online courses. I googled how to fix this issue converting to decimal point for 2 hours. Found this out use 1 of these 2 “AS DECIMAL(8,5)” or “DECIMAL(8,5)” to convert to decimal none of these work for me.
All the percentage columns in the table are all decimal(8,5). I read on Google when you run a SQL query defaults too floating-point. My problem is I do not know how to convert to decimal(8,5), I need 100% accuracy.
I have the table down below and the SQL query down below. Can you please show me how to convert it to decimal.
I have shortened table names to make it look better.
Table
1_player
2_player
3_player
id1_TA
id2_TA
id3_TA
avg_TA
2
16
30
85.00000
100.00000
100.00000
100.00000
10
9
9
100.00000
100.00000
100.00000
100.00000
11
2
15
100.00000
85.00000
100.00000
100.00000
Code
UPDATE test
SET `avg_TA_percent` = (`id1_TA_percent` + `id2_TA_percent`+ `id3_TA_percent`) / 3
WHERE `id1_TA_player` = 2 || `id2_TA_player` = 2 || `id3_TA_player` = 2;
Thank you for your help.
UPDATE test
SET avg_TA = (id1_TA + id2_TA + id3_TA) / 3
WHERE 2 IN (1_player, 2_player, 3_player);
https://dbfiddle.uk/?rdbms=mysql_8.0&fiddle=47baf4517bf96643d6c2e16f5243fc42
Or maybe you mean that now your column are FLOAT and you want to change the datatype and update the average values?
ALTER TABLE test
MODIFY COLUMN id1_TA DECIMAL(8,5),
MODIFY COLUMN id2_TA DECIMAL(8,5),
MODIFY COLUMN id3_TA DECIMAL(8,5),
MODIFY COLUMN avg_TA DECIMAL(8,5);
https://dbfiddle.uk/?rdbms=mysql_8.0&fiddle=f001d75383c039a293055d58c2499550
Think also about changing static averages column to generated one. In this case it will always provide actual value without recalculation:
ALTER TABLE test
MODIFY COLUMN id1_TA DECIMAL(8,5),
MODIFY COLUMN id2_TA DECIMAL(8,5),
MODIFY COLUMN id3_TA DECIMAL(8,5),
DROP COLUMN avg_TA,
ADD COLUMN avg_TA DECIMAL(8,5) AS ((id1_TA + id2_TA + id3_TA) / 3);
https://dbfiddle.uk/?rdbms=mysql_8.0&fiddle=bf086aafc164ec82aaef717ee6d07707
PS. It is impossible to achieve 100% accuracy. None datatype can store, for example, 88.(3) which is 88⅓ - see the last fiddle.
PPS. Column names are taken from the sample data.
I am trying to run a for loop in a R data frame to pull the Last Price of dataframe of stocks. I am having trouble appending the result to the original dataframe and using it as a second column. Here is the code I am working with thus far. I can get it to print but not add to a new column. I tried to set the loop value equal to a new column but I get an error
for (i in df_financials$Ticker){
df_financials$Last_Price=(bdp(i,'PX_LAST'))
}
Error in `$<-.data.frame`(`*tmp*`, "Last_Price", value = list(PX_LAST =
NA_real_)) :
replacement has 1 row, data has 147
Print(df_financials)
Ticker
1 ENH Equity
2 AXS Equity
3 BOH Equity
4 CNA Equity
5 TRH Equity
You first need to specify the order to apply your command to the stated vector and when to stop [i.e., use 1:length(df$Var) within for()]. Second, specify which row (i) of your new column to replace (i.e.,df$var[i]). Give the code below a try and see if that works.
for (i in 1:length(df_financials$Ticker)){
df_financials$Last_Price[i]=(bdp(i,'PX_LAST'))
}
I'm not familiar with the bdp() function itself. However, I suspect the
problem is that you are trying to pull data from a list with more stocks than you are interested in. If this is the case you need to reference the stock in row i that you want to obtain the last price for. If I'm understanding this correctly the code below should do the trick.
I'll assume that the list is something like
Stock<-data.frame(other_stocks = c("ENH","AXS","Rando1","BOH","CNA","TRH","Rando2","Rando3"),
PX_LAST=c(1,2,3,4,5,6,7,8))
Stock
for (i in 1:length(df$Ticker)){
df$Last_Price[i]=(bdp(df$Ticker[i],'PX_LAST'))
}
Setup dataframe
mta<-c("ldall","nold","ldall","nold","ldall","nold","ldall","nold")
mtb<-c(491, 28581,241,5882,365,7398,512,10887)
df1<-data.frame(mta,mtb)
I can order my dataframe in the normal way. This works fine.
df1[order(mtb),]
But if I change the names of the columns
names(df1)<-c("mta1","mtb1")
df1[order(mtb1),]
This gives the error
Error in order(mtb1) : object 'mtb1' not found.
If I use the old column name in the instruction it works, although the output shows the new column name.
df1[order(mtb),]
If I change the name back to the original, the command appears to work normally. Can anyone explain? Is order using a hidden version of the column name?
This should work. Let me know if this helps.
mta<-c("ldall","nold","ldall","nold","ldall","nold","ldall","nold")
mtb<-c(491, 28581,241,5882,365,7398,512,10887)
df1<-data.frame(mta,mtb)
# Change column names
colnames(df1) <- c("mta1","mtb1")
# Sort column mtb1 from the data frame
df1[order(df1$mtb1), ]
mta1 mtb1
3 ldall 241
5 ldall 365
1 ldall 491
7 ldall 512
4 nold 5882
6 nold 7398
8 nold 10887
2 nold 28581
I am trying to write a script that loops through month-end dates and compares associated fields, but I am unable to find a way to way to do this.
I have my data in a flatfile and subset based on 'TheDate'
For instance I have:
date.range <- subset(raw.data, observation_date == theDate)
Say TheDate = 2007-01-31
I want to find the next month included in my data flatfile which is 2007-02-28. How can I reference this in my loop?
I currently have:
date.range.t1 <- subset(raw.data, observation_date == theDate+1)
This doesnt work obviously as my data is not daily.
EDIT:
To make it more clear, my data is like below
ticker observation_date Price
ADB 31/01/2007 1
ALS 31/01/2007 2
ALZ 31/01/2007 3
ADB 28/02/2007 2
ALS 28/02/2007 5
ALZ 28/02/2007 1
I am using a loop so I want to skip from 31/01/2007 to 29/02/2007 by recognising it is the next date, and use that value to subset my data
First get unique values of date like so:
unique_dates<-unique(raw.data$observation_date)
The sort these unique dates:
unique_dates_ordered<-unique_dates[order(as.Date(unique_dates, format="%Y-%m-%d"))]
Now you can subset based on the index of unique_dates_ordered i.e.
subset(raw.data, raw.data$observation_date == unique_dates_ordered[i])
Where i = 1 for the first value, i = 2 for the second value etc.
I want to read a file and calculate the mean of it.
`>list
[1] "book1.csv" "book2.csv".
for book1
observation1
23
24
65
76
34
In books i have a variable observation 1 and observation 2 column for book 1 and 2 respectively. So i want to write a function where i can calculate mean of it.I am new to R and not able subset the variable of books. Can anyone please help me out in writing the function?
Try this. File represents the file to be read in (book1) and the variable represents the variable to take mean over (observation 1)
read.mean<-function(file,variable){
df<-read.csv(file)
mean.df <- mean(df[,variable])
return(mean.df)
}
Make sure to pass your arguments in quotes, i.e. read.mean("book1", "observation1"). There is a way to do it without the quotes (Passing a variable name to a function in R) but it is complicated.