Print all values greater than some value without using booleans in R? - r

anyone got any idea how to print element greater than some value from a set without using booleans?
E.g., suppose I have the set x, which includes the elements (1, 4, 6, 3, 5, 2, 9).
Obviously, we can print all the values of x greater than 5 using the following code:
x[x>5]
But this way of coding uses booleans (it uses TRUE, FALSE etc.)
But is there any way to do this using solely integers?
I was thinking about some sort of loop that would start contain the number of elements, and then do
x[c(variable)]
but I don't know really.
Please help?

Related

Applying combine function over dataframe and saving as a single list/dataframe

I am trying to create a network. I have a dataframe:
. . v1 .v2 .v3
1/ .1 ... 2.... 3
2/ .3 ... 4 .. 7
3/. 6 ...11 . 9
I wish to find all possible combinations within each row. For example, the 1st row has the values 1, 2, 3 so the result would be (1, 2) (1, 3) (2, 3). Then I would go on to find the combinations between values in only the second row 3, 4, 7 returning (3, 4) (3, 7) (4, 7).
Now I wish to do this function by row and then combine all the results to have an edge list of sorts to create the network out of.
I have tried for couple hours iterations of for functions, apply, and combn but I cant seem to get it to work.
Does anyone have any ideas on how to approach this?
Finally, I am relatively new to R. Is there a way I should break it down or do something step by step to solve? I would really appreciate if you could use simple functions like apply, for loops, and stuff rather than odd specific functions. That way I can look over your ideas and learn from them so I can apply them to other problems later.
You were probably very close
(though you should post the code of your attempt in the future,
and also use something like dput for your data).
The problem of using apply is that it will try to simplify automatically
(to something like a single vector or matrix),
and since combn returns matrices,
you definitely don't want simplification.
You should use lapply when you have cases like these:
lapply(1L:nrow(your_data_frame), function(i) { combn(your_data_frame[i,], m=2L) })

Series vector for approximating pi

I've been set a question about Madhava's approximation of pi. The first part of it is to create a vector which contains the first 20 terms in the series. I know I could just input the first 20 terms into a vector, however that seems like a really long winded way of doing things. I was wondering if there is an easier way to create the vector?
Currently I have the vector
g = c((-3)^(-0)/(2*0+1), (-3)^(-1)/(2*1+1), (-3)^(-2)/(2*2+1), (-3)^(-3)/(2*3+1), (-3)^(-4)/(2*4+1), (-3)^(-5)/(2*5+1), (-3)^(-6)/(2*6+1), (-3)^(-7)/(2*7+1), (-3)^(-8)/(2*8+1), (-3)^(-9)/(2*9+1), (-3)^(-10)/(2*10+1), (-3)^(-11)/(2*11+1), (-3)^(-12)/(2*12+1), (-3)^(-13)/(2*13+1), (-3)^(-14)/(2*14+1), (-3)^(-15)/(2*15+1), (-3)^(-16)/(2*16+1), (-3)^(-17)/(2*17+1), (-3)^(-18)/(2*18+1), (-3)^(-19)/(2*19+1), (-3)^(-20)/(2*20+1))
And
h = sqrt(12)
So I have done g*h to get the approximation of pi. Surely there's an easier way of doing this?
Apologies if this is relatively basic, I am very new to R and still learning how to properly use stack overflow.
Thanks.
One of the best features of R is that it is vectorised. This means that we can do operations element-wise on entire vectors rather than having to type out the operation for each element. For example, if you wanted to find the square of the first five natural numbers (starting at one), we can do this:
(1:5)^2
which results in the output
[1] 1 4 9 16 25
instead of having to do this:
c(1^2, 2^2, 3^2, 4^2, 5^2)
which gives the same output.
Applying this amazing property of R to your situation, instead of having to manually construct the whole vector, we can just do this:
series <- sqrt(12) * c(1, -1) / 3^(0:19) / seq(from=1, by=2, length.out=20)
sum(series)
which gives the following output:
[1] 3.141593
and we can see more decimal places by doing this:
sprintf("%0.20f", sum(series))
[1] "3.14159265357140338182"
To explain a little further what I did in that line of code to generate the series:
We want to multiply the entire thing by the square root of 12, hence the sqrt(12), which will be applied to every element of the resulting vector
We need the signs of the series to alternate, which is accomplished via * c(1, -1); this is because of recycling, where R recycles elements of vectors when doing vector operations. It will multiply the first element by one, the second element by -1, then recycle and multiply the third element by 1, the fourth by -1, etc.
We need to divide each element by 1, 3, 9, etc., which is accomplished by / 3^(0:19) which gives / c(3^0, 3^1, ...)
Lastly, we also need to divide by 1, 3, 5, 7, etc. which is accomplished by seq(from=1, by=2, length.out=20) (see help(seq))

Count blocks in a series

This is a simple problem, however I cannot find an elegant solution for:
Given is the following vector series:
series=c(1,2,4,5,6,1,2,4,5,6,7,8,2,4)
I now want to count blocks of this vector in the same vector; e.g. if I have a block size of 2, I would like to count the pairs 1&2, 2&4, 4&5 and so on (in total 8 unique blocks if I did the counting right).
Can you think of an easy way to program that so that I receive an output matrix with a column for the "unique block number" and a corresponding column for the counts?
One idea is to can use rollapply from zoo,
nrow(unique(rollapply(series, 2, by = 1, paste0)))
#[1] 8
You can change '2' to get combinations(block sizes) of 3, 4, etc...

Couldn't reduce the looping variable inside the "for" loop in R

I have a for loop to do a matrix manipulation in R. For some checks are true i need to come to the same row again., means i need to be reduced by 1.
for(i in 1:10)
{
if(some chk)
{
i=i-1
}
}
Actually i is not reduced for me. For an example in 5th row i'm reducing the i to 4, so again it should come as 5, but it is coming as 6.
Please advice.
My intention is:
Checking the first column values of a matrix, if I find any duplicate value, I take the second column value and append with the first row's second column and remove the duplicate row. So, when I'm removing a row I do not need increase the i in while loop. (This is just a map reduce method, append values of same key)
Variables in R for loops are read-only, you cannot modify them. What you have written would be solved completely differently in normal R code – the exact solution depending on the actual problem, there isn’t a generic, direct replacement (except by replacing the whole thing with a while loop but this is both ugly and probably unnecessary).
To illustrate this, consider these two typical examples.
Assume you want to filter all duplicated elements from a list. Instead of looping over the list and copying all duplicated elements, you can use the duplicated function which tells you, for each element, whether it’s a duplicate.
Secondly, you use standard R subsetting syntax to select just those elements which are not a duplicate:
x = x[! duplicated(x)]
(This example works on a one-dimensional vector or list, but it can be generalised to more dimensions.)
For a more complex case, let’s say that you have a vector of numbers and, for every even number in the vector, you want to double the preceding number (this is highly artificial but in signal processing you might face similar problems). In other words:
input = c(1, 3, 2, 5, 6, 7, 1, 8)
output = ???
output
# [1] 1 6 2 10 6 7 2 8
… we want to fill in ???. In the first step, we check which numbers are even:
even = input %% 2 == 0
# [1] FALSE FALSE TRUE FALSE TRUE FALSE FALSE TRUE
Next, we shift the result down – because we want to know whether the next number is even – by removing the first element, and appending a dummy element (FALSE) at the end.
even = c(even[-1], FALSE)
# [1] FALSE TRUE FALSE TRUE FALSE FALSE TRUE FALSE
And now we can multiply just these inputs by two:
output = input
output[even] = output[even] * 2
There, done.

Getting Least Common Equal Sum and Combination of Two Sets of Numbers

I'm currently creating a program in C# that would look for the lowest possible equal sum of two sets of numbers, in which you may repeat the numbers as many times as you want.
For example, I have these two sets { 10, 13, 18 } and { 12, 16, 22 }. The lowest possible sum that I can get is 28: (10 + 18) and (12 + 16).
Another example is {5, 7, 9} and {1, 2, 3}. Lowest possible sum is 5: (5) and (1+1+1+1+1) or (1+2+2) or (2+3) and so on.
Any suggestions on where I can start? I'll actually be using 6 numbers per set and the numbers are in the hundreds / thousands mark.
Maintain two sets, initialized from your input sets, and ordered by increasing numbers (e.g. using a tree-based set structure). Now compare the first (i.e. minimal) elements from the two sets. The smaller one you remove from its set, add all elements from the corresponding input set to that value, and insert the results. When both sets have the same minimal element, then you are done and that element is your least common equal sum.

Resources