How can I generate ascii "graphical output" from R? - r

I believe R can generate stem-and-leaf for ascii histograms, and scatter plots using this code from Matt Shotwell.
Can it also generate ASCII based line graphs, like this from GNUPlot?

You should look at the fairly recent txtplot package. Currently, it includes scatterplot, line plot, density plot, acf, and bar chart.
From the online help,
> txtplot(cars[,1], cars[,2])
+----+------------+------------+-----------+------------+--+
120 + * +
| |
100 + +
| * * |
80 + * * +
| * * * |
60 + * * +
| * * * * * |
40 + * * * * * +
| * * * * * * * |
20 + * * * * * * * +
| * * * * |
| * * * |
0 +----+------------+------------+-----------+------------+--+
5 10 15 20 25

I know there is support for basic interaction between R and GnuPlot in the TeachingDemos package. Perhaps that can achieve what you want.

Related

Compute sequence of summed fractions in R

I want to compute the following sequence using R, without loops, i.e. for cycles.
1 + (2/3) + ((2/3)*(4/5)) + ((2/3)*(4/5)*(6/7)) + ... + ((2/3)*(4/5)*...(20/21))
So far, I tried different approaches with a sequence as well as a while function, but could not came up with a suitable solution. Help would be highly appreciated.
We may use cumprod
v1 <- seq(2, 20, by = 2)
v2 <- seq(3, 21, by = 2)
1 + sum(cumprod(v1/v2))
[1] 4.945724
-manual calculation
1 + (2/3) + ((2/3)*(4/5)) + ((2/3)*(4/5)*(6/7)) + ((2/3)*(4/5)*(6/7) * (8/9)) + ((2/3)*(4/5)*(6/7) * (8/9) * (10/11)) + ((2/3)*(4/5)*(6/7) * (8/9) * (10/11) * (12/13)) + ((2/3)*(4/5)*(6/7) * (8/9) * (10/11) * (12/13) * (14/15)) + ((2/3)*(4/5)*(6/7) * (8/9) * (10/11) * (12/13) * (14/15) * (16/17)) + ((2/3)*(4/5)*(6/7) * (8/9) * (10/11) * (12/13) * (14/15) * (16/17) * (18/19)) + ((2/3)*(4/5)*(6/7) * (8/9) * (10/11) * (12/13) * (14/15) * (16/17) * (18/19) * (20/21))
[1] 4.945724

Math behind normalization of features (fpfh)?

I am trying to get behind the math of how the features are normalized in fpfh.hpp.
nr_bins_f1 * ((pfh_tuple[0] + M_PI) * d_pi_))
nr_bins_f2 * ((pfh_tuple[1] + 1.0) * 0.5))
nr_bins_f3 * ((pfh_tuple[2] + 1.0) * 0.5))
Why is (pfh_tuple[]+M_PI) * d_pi_ or ...+1.0)*0.5?

I need this diamond shape pattern in R

*
* *
* * *
* * * *
* * * * *
* * * *
* * *
* *
*
This is my code so far, but it throws an error:
Error in " " * (rows - i - 1) : non-numeric argument to binary operator
Calls: pyramid -> print
Execution halted
#R version 3
pyramid<-function(rows){for (i in rows){print(" "*(rows-i-1)+"*"*(i+1))}
for(j in (rows-1|0|-1)){print(" "*(rows-j)+"*"*(j))}}
rows<-5
pyramid(rows)
You can find plenty of (pseudo-code) examples on the net. That should've been your first approach towards solving your problem. SO is not a free code writing service, and you'll get a much more positive response if you demonstrate a genuine attempt at solving the problem yourself.
That aside, here is a "crude" R implementation of a code example I found here. The code can and probably should be "R-ified", and I encourage you to spend some time doing so. I promise that you'll learn a lot. For example, it should be possible to replace most (all?) explicit for loops by making use of vectorised functions.
diamond <- function(max) {
# Upper triangle
space <- max - 1
for (i in 0:(max - 1)) {
for (j in 0:space) cat(" ")
for (j in 0:i) cat("* ")
cat("\n")
space <- space - 1
}
# Lower triangle
space = 1;
for (i in (max - 1):1) {
for (j in 0:space) cat(" ")
for (j in 0:(i - 1)) cat("* ")
cat("\n")
space <- space + 1
}
}
diamond(5)
# *
# * *
# * * *
# * * * *
#* * * * *
# * * * *
# * * *
# * *
# *

Truncated warning or error massages

When I try to build a model using PKSFC, I get this message:
[... truncated]
R lists three mistakes and a half (the equations end with *_EC).
I guess I can correct the mistake, but I would like to correct them all in one pass.
I guess there are many more (maybe 15?).
The full message:
5: In sfc.check(vn_mod, fill = FALSE) : Equations
57192.4187750356 + POPAGE_1 * (0.5 * (0 - 55335.9694996733)/POPAGE_1 + 0.2 * (POPAGE - POPAGE_1)/POPAGE_1 - 0.2 * (57192.4187750356 - 0.5 * 55335.9694996733 - 0.508679781654891 * POPAGE_1)/POPAGE_1 + ACPOP_EC)
28.7269160437129 * exp(0.548725 * log(0/1.489181) + 0.704327 * log(0/37.4835480991331) - 0.247704 *
(log(28.7269160437129/37.4835480991331 * (1 + R_SCP_1)) - 0.5 *
log(1.489181) - (1 - 0.5) * log(1.52438302758911)) - 0.5 * 0 -
0.0511046682618056 + WP_EC)
1.25809781599844 * exp(0.2 * log(0/1.46079630578026) + 0.8 * log(PPX * 0/(PPX_1 * 1.1362002193817)) - 0.2 * (log(1.25809781599844) - 0.2 * log(1.46079630578026) - (1 - 0.2) * log(PPX_1 * 1.1362002193817)) -
0.00325177611673638 * (T - 2016) * (T <= 2016) - 0.0576138445722962 + PM_EC)
1.30468806319261 * exp(0.7 * log(0/1.46079630578026) + 0.3 * log(PPX * 0/(PPX_1 * 1.1362002193817)) - 0.2 * (log(1.30468806319261) - 0.7 * log(1.46079630578026) - (1 - 0.7) * log(PPX_1 * 1.1362002193817)) -
0.0033333860060119 * (T - 201 [... truncated]

Maths help with exponentials [closed]

Closed. This question is off-topic. It is not currently accepting answers.
Want to improve this question? Update the question so it's on-topic for Stack Overflow.
Closed 11 years ago.
Improve this question
Evaluate
(z x^-1 y)^5 y^5
~~~~~~~~~~~~~~~~~~~~~~~ OVER
x^-4 z^-4
How would I evaluate this if X = 10, y = -3 and z = 3? I would like a step-by-step solution to help me fully understand it.
Numerator evaluates as (z*y*x^-1)^5 * y^5
further rewriting ((z^5*y^5)*y^5)/x^5
Denominator ((1/x^4)*(1/z^4))
Final Answer would be ((y^10)*(z^9))/x
as per your values it (3^19)/10
Exponentials have higher priority in most computer languages, so adding parentheses like this should make it clearer. I'm assuming that you're dividing the first polynomial by the second. It's simple algebra.
(z x^-1 y)^5 y^5
---------------- =
x^-4 z^-4
(y^10)(z^9)/x
You substitute the numbers.
Start with:
((z * x^-1 * y)^5 * y^5)/(x^-4 * z^-4)
Commute the exponent to the z factor: (A * B)^N => A^N * B^N
(z^5 * (x^-1 * y)^5 * y^5)/(x^-4 * z^-4)
Commute the exponent to the x and y factors: (A * B)^N => A^N * B^N
(z^5 * (x^-1)^5 * y^5 * y^5)/(x^-4 * z^-4)
Simplify the exponenet on the x factor: (A^N)^M => A^(N*M)
(z^5 * x^-5 * y^5 * y^5)/(x^-4 * z^-4)
Combine the y factors: A^N * A^M => A^(N+M)
(z^5 * x^-5 * y^10)/(x^-4 * z^-4)
Remove the negative exponent on x: 1/A^-N => A^N
(z^5 * x^-5 * y^10 * x^4) / (z^-4)
Remove the negative exponenet on z: 1/A^-N => A^N
z^5 * x^-5 * y^10 * x^4 * z^4
Combine the z factors: A^N * A^M => A^(N+M)
z^9 * x^-5 * y^10 * x^4
Combine the x factors: A^N * A^M => A^(N+M)
z^9 * x^-1 * y^10
Remove the negative exponent on x: A^(-N) => 1/A^N
(z^9 * y^10)/(x^1)
Simplify the x factor: A^1 => A
(z^9 * y^10)/(x)
And that's the algebraic form of your answer.
Next, subsitute the values:
3^9 * (-3)^10 / 10
Factor the exponents:
(3^3)^3 * (-3)^10 / 10
(3^3)^3 * ((-3)^2)^5 / 10
Evalutate the innermost exponents:
(3 * 3 * 3)^3 * ((-3)^2)^5 / 10
(9 * 3)^3 * ((-3)^2)^5 / 10
27^3 * ((-3)^2)^5 / 10
27^3 * 9^5 / 10
Continue evaluation exponents, breaking them down for simplicity:
27 * 27 * 27 * 9^5 / 10
27 * 27 * 27 * 9^5 / 10
729 * 27 * 9^5 / 10
19683 * 9^5 / 10
19683 * 9^2 * 9^2 * 9 / 10
19683 * 81 * 81 * 9 / 10
Then multiply the factors:
19683 * 81 * 729 / 10
19683 * 59049 / 10
1162261467 / 10
116226146.7
And there's your final answer.
You could also take advantage of the fact that X^N = (-X)^N if N is even by replacing -3 with 3 since 10 is even.
3^9 * (-3)^10 / 10
3^9 * 3^10 / 10
3^19 / 10
3 * 3^18 / 10
3 * (3^9)^2 / 10
3 * (3 * 3^8)^2 / 10
3 * (3 * (3^2)^4)^2 / 10
3 * (3 * ((3^2)^2)^2)^2 / 10
3 * (3 * (9^2)^2)^2 / 10
3 * (3 * 81^2)^2 / 10
3 * (3 * 6561)^2 / 10
3 * (19683)^2 / 10
3 * 387420489 / 10
1162261467 / 10
116226146.7

Resources