My OS - Fedora
QtCreator Qt4.8
My Qtablewidget has floats in 50 rows in column 2
I am trying to sum the values in those 50 cells, my return local is 'nan' I'm puzzled.
the data looks like this which was generated by the qDebug << tlocal;
0
0
0
0
0.11
0.062
0.12
0.11
0.21
0.22
0.11
0.15
0.24
0.14
0.046
0.16
0.36
0.14
0
0.015
0.015
0.13
0.1
0.0197
0.0262
0.14
0.04
0.001
0.0002
0.002
0.00016
1.18e-05
2.96e-05
3.01205e-05
4.5e-07
0.001
5.4e-05
7.4e-08
1.71e-07
4.9e-08
5.7e-07
4.9e-07
1.15e-12
8.9e-12
0
5.6e-05
My function is here
float MainWindow::sum_requirements()
{
float local;
float tlocal = 0;
bool ok;
for (int i=0; i < ui->tableWidget->rowCount(); ++i)
{
tlocal = ui->tableWidget->item(i, 2)->text().toFloat(&ok);
qDebug() << tlocal;
local = local + tlocal;
}
qDebug() << "Before return = " << local;
return local;
}
float local; lacks an initial value.
Use float local = 0;.
Naming: consider renaming local to sum to add clairty.
Related
This is an edited post (awk,sed method to combine multiple files to one by mapping to a common file/column)
Due to my naivety the last post was not in the correct format and was closed down before getting a correct answer. For some unknown reason, I could not edit/delete the said post.
Sorry for the trouble. Hope this is the correct format.
I have 11 (tab-separated) files each with two columns as shown below. the row number varies with some files having 1000 rows while some above 2500.
File-0
This is the mapping file
K00001 0
K00002 0
K00003 0
K00004 0
K00005 0
This file goes up to K30000 0 covering all Knumbers in the rest of the file
File-1
K00002 0.60
K00003 31
K00006 0.21
K00007 0.06
K00012 0.01
File-2
K00003 21
K00004 0.54
K00005 0.4
K00006 0.01
K00009 0.39
K00010 0.01
File-3
K00002 09
K00003 0.11
K00004 0.87
K00006 0.54
K00007 0.11
K00008 0.02
I want to combine all these 10 files (file-1....file-10) into one by mapping to the first column of file-0. The output I would like will be like this:
K00001
K00002 0.60 9
K00003 31 21 0.11
K00004 0.54 0.87
K00005 0.4
K00006 0.21 0.01 0.54
K00007 0.06 0.11
K00008 0.02
K00009 0.39
K000010 0.01
K000011
K00012 0.01
Can anyone help me with this?
Thanks.
Looking at your last post I believe #EdMorten's AWK answer is the solution you are looking for - you just need to change the field separator from "\t" to " ":
# To get the first column of the mapping file (i.e. lose the column of zeros):
cut -d" " -f1 file0 > test1.txt
cat test1.txt
K00001
K00002
K00003
K00004
K00005
K00006
K00007
K00008
K00009
K00010
K00011
K00012
K00013
cat test2.txt
K00002 0.60
K00003 31
K00006 0.21
K00007 0.06
K00012 0.01
cat test3.txt
K00003 21
K00004 0.54
K00005 0.4
K00006 0.01
K00009 0.39
K00010 0.01
cat test4.txt
K00002 09
K00003 0.11
K00004 0.87
K00006 0.54
K00007 0.11
K00008 0.02
awk '
BEGIN { FS=OFS=" " }
{ map[$1][ARGIND] = $2 }
END {
PROCINFO["sorted_in"] = "#ind_str_asc"
for (key in map) {
printf "%s", key
for (fileNr=1; fileNr<=ARGIND; fileNr++) {
printf "%s%s", OFS, map[key][fileNr]
}
print ""
}
}
' test*.txt
K00001
K00002 0.60 09
K00003 31 21 0.11
K00004 0.54 0.87
K00005 0.4
K00006 0.21 0.01 0.54
K00007 0.06 0.11
K00008 0.02
K00009 0.39
K00010 0.01
K00011
K00012 0.01
K00013
Does this work on your "real" data?
I'm trying to extract table text from pdf file which is wrote in Korean.
I used library which named tabulizer to extract text.
So my code is
library(pdftools)
library(tidytext)
library(dplyr)
library(janeaustenr)
library(rJava)
library(tabulizer)
library(tidyverse)
setwd("C:/Users/user/Desktop/Test") #This is my directory which contain pdf files.
files <- list.files(pattern = "pdf$")
f2 <- files[6]
e <- extract_text(f2,pages = 25,encoding = 'UTF-8')
But the problem is the table which in the pdf file, the pattern is not desirable for me.
I want extract data vertically, however, the extract_text function makes strings horizontally
Here is the outcome which occurred with extract_text:
나. 집합투자기구에 부과되는 보수 및 비용 \r\n구분 \r\n지급비율(연간, %) \r\n지급시기 \r\nC(수수료미징\r\n구-오프라인) \r\nW(수수료미징\r\n구-오프라인-\r\n랩) \r\ne(수수료미징구\r\n-온라인) \r\nI(수수료미징구-\r\n오프라인-기관) \r\nC-P(수수료미\r\n징구-오프라인-\r\n개인연금) \r\nC-P2(수수료미\r\n징구-오프라인-\r\n퇴직연금) \r\n집합투자업자 보수 0.46 0.46 0.46 0.46 0.46 0.46 \r\n매 3개월 \r\n판매회사 보수 1.00 0.00 0.98 0.03 0.95 0.85 \r\n수탁회사 보수 0.025 0.025 0.025 0.025 0.025 0.025 \r\n일반사무관리회사 보\r\n수 \r\n0.005 0.005 0.005 0.005 0.005 0.005 \r\n총 보수 1.49 0.49 1.47 0.52 1.44 1.34 - \r\n기타비용 0.002 0.002 0.002 0.002 0.002 0.002 사유 발생 시 \r\n총 보수․비용 1.492 0.492 1.472 0.522 1.442 1.342 - \r\n(동종유형 총 보수) 1.59 - 1.22 - - - - \r\n총 보수․비용 \r\n(피투자 집합투자기구 보수 포함) 1.493 0.493 1.473 0.523 1.443 1.343 - \r\n증권거래비용 0.107 0.108 0.105 0.108 0.106 0.104 사유 발생 시 \r\n구분 지급비율(연간, %) 지급시기 \r\n"
And more specifically I attached capture image.
Again, what I want to extract is something vertical (red circle)
But extract_text organize it horizontally (blue circle)
Also if you know how can organize the text like {cat(e, sep="\n")} please leave a comment, because using the cat function, I cannot contain result in variables, it automatically gives me output and I have no choice to contain these values.. but i want things clearly organized and 'Anytime i want to need some information I go to the container variables, then get the info..' << that's what I need
Let X be a 3d-array with size r*c*n and y be a vector of length n with two levels. I want to compute the mean matrix of X grouped by y. Here I try to define the function using arma::cube, but every time calling the function, the results returned are different and very strange. Even for very small (r, c, n), the results always include NaN.
The content of .cpp file is as below:
// [[Rcpp::depends(RcppArmadillo)]]
#include <RcppArmadillo.h>
using namespace Rcpp;
// [[Rcpp::export]]
List f(arma::cube X, CharacterVector y){
unsigned int n = X.n_slices;
unsigned int rNums = X.n_rows;
unsigned int cNums = X.n_cols;
arma::mat mu1(rNums, cNums);
arma::mat mu2(rNums, cNums);
unsigned int n1 = 0;
unsigned int n2 = 0;
CharacterVector yLevels = sort_unique(y);
for(unsigned int i=0; i < y.length(); i++){
if(y[i] == yLevels[0]) {
mu1 += X.slice(i);
n1++;
} else {
mu2 += X.slice(i);
n2++;
}
}
mu1 /= n1;
mu2 /= n2;
return Rcpp::List::create(Named("mu1") = mu1,
Named("mu2") = mu2);
}
Then I call this .cpp file in R and call the function of both R and Cxx versions as followed:
> rm(list=ls())
> options(digits=2)
> library(Rcpp)
> sourceCpp("Cxx_File.cpp")
>
> set.seed(2018)
> X <- array(rnorm(4*5*10), dim=c(4, 5, 10))
> y <- c(rep("1", 4), rep("2", 6))
>
> f(X, y)
$mu1
[,1] [,2] [,3] [,4] [,5]
[1,] 0.33 0.076 0.230 0.43 -0.801
[2,] -0.50 -0.145 0.162 -0.21 0.629
[3,] -0.13 0.516 -0.266 -0.37 -0.261
[4,] 0.73 0.226 -0.071 -0.36 0.035
$mu2
[,1] [,2] [,3] [,4] [,5]
[1,] 0.098 0.016 0.27 0.70 0.017
[2,] 0.434 -0.164 0.40 0.77 0.104
[3,] 0.060 0.226 0.05 NaN 0.091
[4,] 0.261 0.313 -0.47 0.18 -0.301
the result conains NaN which shouldnot have appeared. What is the reason?
That has to do with the fact that arma::mat does not initialize matrices with zeros. Add mu1.fill(0), mu2.fill(0) and sigma2.fill(0) when you initialize these variables.
I struggling with reading non-uniform data into R.
I've achieved the following:
Used "readLines" to read the text file data in
Used "grep" to find the block of data that I want
Used the index from grep to create a variable (named "block") that contains only that block of data
All good so far - I now have the data I want. But - its a character variable with only one column that contains all the data.
This creates a sample of the variable I have made called "block" (first 3 rows):
line1 = c(" 114.24 -0.39 0.06 13.85 -0.06 1402.11 -1.48 0.0003 0.0000 35.468 1.02 -0.02 0.00 0 1 1 1 0 49.87 4 -290 0 0 -0.002 -0.010 0.155 999.00 11482.66 999.00 11482.66 16:52:24:119 255 13.89 50.00 0.00 -5.49 0.00")
line2 = c(" 114.28 -0.39 0.08 13.84 -0.06 1402.57 -1.48 0.0004 0.0000 35.479 1.29 -0.02 0.00 0 1 1 1 0 49.82 4 -272 0 0 -0.002 -0.011 0.124 999.00 11482.66 999.00 11482.66 16:52:24:150 255 13.89 50.00 0.00 -5.49 0.00")
line3 = c(" 114.31 -0.39 0.09 13.83 -0.06 1403.03 -1.47 0.0005 0.0000 35.492 1.42 -0.02 0.00 0 1 1 1 0 49.78 4 -263 0 0 -0.002 -0.011 0.046 999.00 11482.66 999.00 11482.66 16:52:24:197 255 13.89 50.00 0.00 -5.49 0.00")
block = c(line1,line2,line3)
My goal is to have this data as a data.frame with separate columns for each data point.
My attempts at using strsplit haven't helped (does the solution involve strsplit?)- what is the best approach here? Any suggestions/feedback welcome.
strsplit(block,"\s",fixed=F)
Either of the following should work for you:
## Creates a "data.table"
library(splitstackshape)
cSplit(data.table(x = block), "x", " ")
## Creates a "data.frame"
read.table(text = block, header = FALSE)
## Creates a character matrix
do.call(rbind, strsplit(block, "\\s+"))
## Like the above, but likely to be faster
library(stringi)
stri_split_regex(block, "\\s+", simplify = TRUE)
Note the "\\s+" for the last two options. The "+" is to match multiple spaces.
Actually - this looks like it might work.
Import raw data into R
But wanted to check if this was the best approach to this situation...?
I have a nasty problem that bugs me a lot.
I have a list (dataframe) that looks like this:
a b c
1 1.00234 1.05667 1.00198
I want to round the numbers of this dataframe to two decimal number.
But the trailing zeros have to be kept like the following:
a b c
1 1.00 1.06 1.00
I tried the round and printf() and so on, it doesn't work, because my data is a list. It can't be coerced. However, I'd like to keep my data structure.
Anyone of you know how to solve this? I appreciate it very much!!!
Here you go:
df <- data.frame(a=1.00234, b=1.0567, c=1.00198, d=99999, e=.00001)
format(round(df, 2), nsmall=2)
# a b c d e
# 1 1.00 1.06 1.00 99999.00 0.00
You can use sapply to effectively loop the rounding over the columns. I'm having trouble determining exactly what your data looks like. But if it's a data.frame with numerous rows and columns, see this example. You'll need to convert back to class data.frame if that matters, as this method of sapply will coerce the data frame to a matrix.
> dd <- data.frame(a = runif(6), b = runif(6), c = rnorm(6))
> dd
a b c
1 0.3992252 0.9905755 -0.2557345
2 0.5052276 0.7990887 -0.7557547
3 0.3215714 0.1134675 -0.4389722
4 0.1794793 0.5372685 1.1657751
5 0.9543305 0.8908360 -1.5966621
6 0.9525730 0.5991279 -0.4819168
> as.data.frame(sapply(dd, round, 2))
a b c
1 0.40 0.99 -0.26
2 0.51 0.80 -0.76
3 0.32 0.11 -0.44
4 0.18 0.54 1.17
5 0.95 0.89 -1.60
6 0.95 0.60 -0.48
#include<iostream>
#include<conio.h>
#include<math.h>
using namespace std;
int main()
{
float a=1.00634;
float p=round(a*100); float q=p/100;
printf("%.2f",q);
getch();
return 0;
}