read write file to 5 decimal places in borland c++ - writetofile

I writing with borland C++ compiler. I want to read numbers with 5 decimal places from text file then save it into another text file.
I wrote my program as below:
main
{
double Bid[25], Be1[25],Bn1[25];
int i,no=10,j,p;
char filename[30];
ifstream fp1;
fp1.open("bufile2.txt");
cout<<"\n id lat long";
for (i=1;i<=no;i++)
{
fp1>>Bid[i]>>Be1[i]>>Bn1[i];
cout<<"\n "<<i<<" "<<Bid[i]<<" "<<Be1[i]<<" "<<Bn1[i];
}
fp1.close();
getch();
return 0;
}
my input file is as follow:
id lat long
1 101.28411 2.91245
2 101.28211 2.91345
3 101.28301 2.91345
4 101.28401 2.91345
5 101.28501 2.91345
6 101.28001 2.91445
7 101.28101 2.91445
8 101.28201 2.91445
9 101.28301 2.91445
10 101.28401 2.91445
my problem is, the Be1[i]... not outputting 5 decimal number in my stored file "result.txt". It only give me 3 decimal places. The result is as followed:
id lat long
1 101.284 2.91245
2 101.282 2.91345
3 101.283 2.91345
4 101.284 2.91345
5 101.285 2.91345
6 101.280 2.91445
7 101.281 2.91445
8 101.282 2.91445
9 101.283 2.91445
10 101.284 2.91445
Help me please

Related

Different ways of indexing dataframe in R

Say, I have a dataframe df in R as follows,
id inflam
1 1 0.03093764
2 2 0.50115406
3 3 0.82153770
4 4 0.01985961
5 5 0.04994588
6 6 0.91714810
7 7 0.83438400
8 8 0.80832225
9 9 0.12360681
10 10 0.08490079
I can access the entirety of the inflam column by indexing as df[,2] or df[2]. However, typeof(df[,2]) returns double, whereas typeof(df[2]) returns list. The comma seems to be the differentiator, but why is this the case? What is going on under the hood?

input$(variable storing ID) not working but input$(ID) working

I have been writing a program using shiny where I need to access the fileInput from the user. Here
is a code exert.
for (machine in 1:machineCount)
{
curFileID <- paste0("file", machine)
print(paste0("Current File ID: ", curFileID))
print(paste0("Current File ID input accessed through variable: ", input$curFileID))
print(paste0("Current File ID input accessed directly through variable name:", input$file1))
}
and here is the output
*[1] "Current File ID: file1"
[1] "Current File ID input accessed through variable: "
[1] "Current File ID input accessed directly through file ID:COPYING.txt"
[2] "Current File ID input accessed directly through file ID:35727"
[3] "Current File ID input accessed directly through file ID:"
[4] "Current File ID input accessed directly through file ID:C:\Users\fabma\AppData\Local\Temp\RtmpYV0PCn"*
You can see that when I used a variable to store the ID it does not work and input$variable returns NULL. However when I directly access with input$fileID then it works. I don't understand why this happens and how to solve it.
curFileID is character variable where you store the name of the column. You cannot directly access the column by using $.
See for example,
mtcars$cyl
#[1] 6 6 4 6 8 6 8 4 4 6 6 8 8 8 8 8 8 4 4 4 4 8 8 8 8 4 4 4 8 6 8 4
temp <- "cyl"
mtcars$temp
#NULL
To access the column values as vector with temp you need to use double brackets [[
mtcars[[temp]]
#[1] 6 6 4 6 8 6 8 4 4 6 6 8 8 8 8 8 8 4 4 4 4 8 8 8 8 4 4 4 8 6 8 4
You can read more about how [, [[ and $ works at ?Extract.

Create a Table from a Data frame with some conditions

From the following data frame, I am trying to output two tables, one for PASS and another one for FAIL. The condition is that the output for each table should contain only the ID and the Score. Can anyone help me with this? I am still starting to know the full capabilities of the table function. If anyone could suggest other alternatives I would greatly appreciate it as long as the conditions for the output is met.
> df <- data.frame(
ID <- as.factor(c(20260, 11893, 54216, 11716, 53368, 46196, 40007, 20970, 11802, 46166, 23615, 11865, 16138, 64789, 43211, 66539));
Score <- c(9,7,6,2,10,7,8,10,6,7,7,9,9,9,10,8)
Remark<- as.factor(c("PASS","PASS","FAIL","FAIL","PASS","PASS","PASS","PASS","FAIL","PASS","PASS","PASS","PASS","PASS","PASS","PASS"))
)
> df
ID Score Remark
1 20260 9 PASS
2 11893 7 PASS
3 54216 6 FAIL
4 11716 2 FAIL
5 53368 10 PASS
6 46196 7 PASS
7 40007 8 PASS
8 20970 10 PASS
9 11802 6 FAIL
10 46166 7 PASS
11 23615 7 PASS
12 11865 9 PASS
13 16138 9 PASS
14 64789 9 PASS
15 43211 10 PASS
16 66539 8 PASS
Something like this?
df <- data.frame(
ID = as.factor(c(20260, 11893, 54216, 11716, 53368, 46196, 40007, 20970, 11802, 46166, 23615, 11865, 16138, 64789, 43211, 66539)),
Score = c(9,7,6,2,10,7,8,10,6,7,7,9,9,9,10,8),
Remark = as.factor(c("PASS","PASS","FAIL","FAIL","PASS","PASS","PASS","PASS","FAIL","PASS","PASS","PASS","PASS","PASS","PASS","PASS"))
)
df[df$Remark == "PASS", 1:2]
ID Score
1 20260 9
2 11893 7
5 53368 10
6 46196 7
7 40007 8
8 20970 10
10 46166 7
11 23615 7
12 11865 9
13 16138 9
14 64789 9
15 43211 10
16 66539 8

Extracting data from dataframe using different dataframe without headers (R)

I have a gridded data as a data-frame that has daily temperatures (in K) for 30 years. I need to extract data for days that matches another data-frame and keep the first and second columns (lon and lat).
Data example:
gridded data from which I need to remove days that do not match days in the second data (df2$Dates)
>head(Daily.df)
lon lat 1991-05-01 1991-05-02 1991-05-03 1991-05-04 1991-05-05 1991-05-06 1991-05-07 1991-05-08 1991-05-09
1 5.000 60 278.2488 280.1225 280.3909 279.4138 276.6809 276.2085 276.6250 277.7930 276.9693
2 5.125 60 278.2514 280.1049 280.3789 279.4395 276.7141 276.2467 276.6571 277.8264 277.0225
3 5.250 60 278.2529 280.0871 280.3648 279.4634 276.7437 276.2849 276.6918 277.8608 277.0740
4 5.375 60 278.2537 280.0687 280.3488 279.4858 276.7691 276.3238 276.7289 277.8960 277.1232
5 5.500 60 278.2537 280.0493 280.3319 279.5066 276.7909 276.3633 276.7688 277.9313 277.1701
6 5.625 60 278.2539 280.0294 280.3143 279.5264 276.8090 276.4042 276.8111 277.9666 277.2147
1991-05-10 1991-05-11 1991-05-12 1991-05-13 1991-05-14 1991-05-15 1991-05-16 1991-05-17 1991-05-18 1991-05-19
1 276.9616 277.3436 273.3149 274.4931 274.6967 275.6298 272.2511 271.5413 271.7289 271.7964
2 276.9689 277.2988 273.3689 274.5399 274.6801 275.6307 272.2214 271.4445 271.6410 271.7023
3 276.9720 277.2533 273.4225 274.5811 274.6646 275.6241 272.1858 271.3391 271.5424 271.5989
4 276.9716 277.2080 273.4726 274.6146 274.6507 275.6109 272.1456 271.2274 271.4340 271.4872
5 276.9689 277.1632 273.5163 274.6382 274.6380 275.5917 272.1022 271.1121 271.3168 271.3693
6 276.9645 277.1190 273.5507 274.6501 274.6263 275.5672 272.0571 270.9955 271.1919 271.2469
1991-05-20 1991-05-21 1991-05-22 1991-05-23 1991-05-24 1991-05-25 1991-05-26 1991-05-27 1991-05-28 1991-05-29
1 272.2633 268.0039 268.5981 269.4139 267.7836 265.8771 263.5669 266.1666 269.7285 272.5083
2 272.2543 268.0218 268.5847 269.4107 267.7886 265.8743 263.5125 266.1031 269.6471 272.4676
3 272.2434 268.0369 268.5716 269.4089 267.7910 265.8669 263.4592 266.0332 269.5697 272.4217
4 272.2308 268.0507 268.5597 269.4090 267.7925 265.8559 263.4066 265.9581 269.4987 272.3714
5 272.2164 268.0642 268.5505 269.4112 267.7936 265.8425 263.3546 265.8797 269.4355 272.3175
6 272.2005 268.0793 268.5451 269.4154 267.7962 265.8276 263.3039 265.7997 269.3818 272.2614
1991-05-30 1991-05-31 1991-06-01 1991-06-02 1991-06-03 1991-06-04 1991-06-05 1991-06-06 1991-06-07 1991-06-08
1 274.2950 273.4715 274.5197 274.7548 273.8259 272.4433 274.1811 274.4135 274.3999 276.0327
2 274.2205 273.4638 274.5292 274.8316 273.8658 272.4700 274.1992 274.4426 274.4650 276.0698
3 274.1421 273.4549 274.5373 274.9027 273.9028 272.4980 274.2160 274.4781 274.5309 276.1012
4 274.0609 273.4452 274.5438 274.9665 273.9365 272.5273 274.2322 274.5211 274.5969 276.1255
5 273.9784 273.4353 274.5482 275.0216 273.9660 272.5576 274.2481 274.5725 274.6617 276.1417
6 273.8960 273.4253 274.5508 275.0668 273.9912 272.5887 274.2649 274.6334 274.7239 276.1487
1991-06-09 1991-06-10 1991-06-11 1991-06-12 1991-06-13 1991-06-14 1991-06-15 1991-06-16 1991-06-17 1991-06-18
1 276.5216 277.1812 277.8093 278.3013 278.5323 278.5403 277.9563 278.3461 275.8296 273.8277
2 276.5531 277.1925 277.8261 278.3409 278.4956 278.5317 277.9148 278.3234 275.8167 273.8302
3 276.5861 277.2065 277.8457 278.3748 278.4503 278.5181 277.8654 278.2939 275.8057 273.8358
4 276.6204 277.2239 277.8684 278.4029 278.3988 278.4996 277.8080 278.2583 275.7966 273.8427
5 276.6564 277.2466 277.8945 278.4253 278.3423 278.4759 277.7429 278.2171 275.7888 273.8504
6 276.6938 277.2753 277.9242 278.4414 278.2834 278.4472 277.6715 278.1714 275.7819 273.8570
1991-06-19 1991-06-20 1991-06-21 1991-06-22 1991-06-23 1991-06-24 1991-06-25 1991-06-26 1991-06-27 1991-06-28
1 275.1738 274.6805 275.6100 274.8936 273.5818 273.2099 273.1788 271.2747 273.2458 276.9931
2 275.1808 274.7123 275.7043 274.9494 273.5861 273.1770 273.2280 271.2435 273.2662 276.9822
3 275.1859 274.7478 275.7993 275.0009 273.5956 273.1439 273.2730 271.2133 273.2803 276.9678
4 275.1891 274.7879 275.8941 275.0467 273.6107 273.1106 273.3130 271.1840 273.2886 276.9502
5 275.1902 274.8337 275.9870 275.0857 273.6318 273.0777 273.3472 271.1556 273.2918 276.9307
6 275.1891 274.8864 276.0776 275.1168 273.6589 273.0454 273.3752 271.1285 273.2905 276.9101
1991-06-29 1991-06-30
1 272.0784 273.5677
2 272.0577 273.5973
3 272.0339 273.6237
4 272.0075 273.6476
5 271.9794 273.6701
6 271.9500 273.6925
Second data I'm using for extracting (using Dates variable)
>head(df2)
Dates Temp Wind.S Wind.D
1 5/1/1991 18 4 238
2 5/2/1991 18 8 93
3 5/4/1991 22 8 229
4 5/6/1991 21 4 81
5 5/7/1991 21 8 192
6 5/9/1991 17 8 32
7 5/13/1991 22 8 229
8 5/18/1991 21 4 81
9 6/2/1991 21 8 192
10 6/7/1991 17 8 32
The header of the final data I'm looking for is something like this:
>head(df3)
lon lat 1991-05-01 1991-05-02 1991-05-04 1991-05-06 1991-05-09 1991-05-13
Example data following the format of yours
Daily.df <- data.frame(lon=1:5,lat=1:5,A=1:5,B=1:5,C=1:5,D=1:5)
colnames(Daily.df) <- c("lon","lat","1991-05-01","1991-05-02","1991-05-03","1991-05-04")
lon lat 1991-05-01 1991-05-02 1991-05-03 1991-05-04
1 1 1 1 1 1 1
2 2 2 2 2 2 2
3 3 3 3 3 3 3
4 4 4 4 4 4 4
5 5 5 5 5 5 5
df2 <- data.frame(Dates = c("5/1/1991","5/2/1991","5/4/1991"))
Dates
1 5/1/1991
2 5/2/1991
3 5/4/1991
Using lubridate to convert df2$Dates into the right format, make a vector of the column names you want to keep (thesedates) including lon and lat. Then use select_at to keep those columns.
library(lubridate)
library(dplyr)
thesedates <- c("lon","lat",as.character(mdy(df2$Dates)))
new.df <- Daily.df %>%
select_at(vars(thesedates))
Output
lon lat 1991-05-01 1991-05-02 1991-05-04
1 1 1 1 1 1
2 2 2 2 2 2
3 3 3 3 3 3
4 4 4 4 4 4
5 5 5 5 5 5
If you want to have a long data set to match, I would think you need to first convert the dates in df2 into the proper format and then wrangle the data into wide format.
Step 1 - Convert dates into correct format
df2$Dates <- as.Date(df2$Dates, format = "%m/%d/%Y")
Step 2 - convert to wide format
library(tidyr)
spread(df2, Dates, data)

Textbox in xlsxwriter

I am trying to insert a textbox into a chartsheet using xlsxwriter in python 3.4 to give more information about the chart. Is there a way to insert a textbox into a chartsheet? I could insert it into a worksheet and not a chartsheet.
Also, is there a way to edit the legend using xlsxwriter? I have a row of 30 values and I want to plot in a batch of 10
batch1 1 2 3 4 5 6 7 8 9 10 1 2 3 4 5 6 7 8 9 10 1 2 3 4 5 6 7 8 9 10
I dont want "batch1" to be shown in the legend 3 times. I want to customize it as iteration1,iteration2,iteration3. Is there a way?
Inserting a textbox into a chart, or chartsheet, isn't supported.
In relation to the second question, you can remove items from the legend in XlsxWriter using the delete_series feature of set_legend():
# Delete/hide series index 0 and 2 from the legend.
chart.set_legend({'delete_series': [0, 2]})
See the docs on set_legend()

Resources