How get numbers from given range with given condition in R - r

I want to get all numbers that are greater than 0 and lesser than 1e6 and does not contain digit 4. How is that possible, please?
My try was:
library(prob)
A <- c(0:(1e6-1))
V <- subset(A, /*I don't know what to put here*/)
But I don't know how to state that I want all numbers that does not contain digit 4....

You could use grep to find out indices with numbers containing 4 and remove them with negative subsetting.
A = 0:1e6
V = A[-grep(4,A)]

Related

How to find the length of a list based on a condition in R

The problem
I would like to find a length of a list.
The expected output
I would like to find the length based on a condition.
Example
Suppose that I have a list of 4 elements as follows:
myve <–list(1,2,3,0)
Here I have 4 elements, one of them is zero. How can I find the length by extracting the zero values? Then, if the length is > 1I would like to substruct one. That is:
If the length is 4 then, I would like to have 4-1=3. So, the output should be 3.
Note
Please note that I am working with a problem where the zero values may be changed from one case to another. For example, For the first list may I have only one 0 value, while for the second list may I have 2 or 3 zero values.
The values are always positive or zero.
You just need to apply the condition to each element. This will produce a list of boolean, then you sum it to get the number of True elements (i.e. validation your condition).
In your case:
sum(myve != 0)
In a more complex case, where the confition is expressed by a function f:
sapply(myve, f)
Use sapply to extract the ones different to zeros and sum to count them
sum(sapply(myve, function(x) x!=0))

Convert a one column matrix to n x c matrix

I have a (nxc+n+c) by 1 matrix. And I want to deselect the last n+c rows and convert the rest into a nxc matrix. Below is what I've tried, but it returns a matrix with every element the same in one row. I'm not sure why is this. Could someone help me out please?
tmp=x[1:n*c,]
Membership <- matrix(tmp, nrow=n, ncol=c)
You have a vector x of length n*c + n + c, when you do the extract, you put a comma in your code.
You should do tmp=x[1:(n*c)].
Notice the importance of parenthesis, since if you do tmp=x[1:n*c], it will take the range from 1 to n, multiply it by c - giving a new range and then extract based on this new range.
For example, you want to avoid:
(1:100)[1:5*5]
[1] 5 10 15 20 25
You can also do without messing up your head with indexing:
matrix(head(x, n*c), ncol=c)

Make vector with 2 elements with equal chance in R

I want to create an R vector with two repeat elements. A length of the array is 200.
But each element can be either 'x' or 'y'.
an element can be x or y with equal chance.
Is there any grammatical function in R to do above task?
Please someone help.
A possible way to do it is to use rbinom. Step by step, generate first a vecotr of 0 and 1, then change it into x and y:
vec = ifelse(rbinom(200, 1, 0.5)==0,"x","y"))
We need a little bit more information to be helpful, but if you want a vector of 200 values, 100 x's and 100 y's, then just do this:
t <- rep(c('X','Y'), 100)
If you want this in a random order:
t <- sample(t)

Drop row in a data frame if value has less than 2 decimal places

In one column of a data frame, I have values for longitude. For example:
df<-data.frame(long=c(-169.42000,144.80000,7.41139,-63.07000,-62.21000,14.48333,56.99900))
I want to keep rows which have at least three decimal places (i.e three non-zero values immediately after the decimal point) and delete all others. So rows 1,2,4 and 5 would be deleted from df in the example above.
So far I've tried usinggrep to extract the rows I want to keep:
new.df<-df[-grep("000$",df$long),]
However this has deleted all rows. Any ideas? I'm new to using grep so there may be glaring errors that I've not picked up on!
Many thanks!
I wouldn't use regex for this.
tol <- .Machine$double.eps ^ 0.5
#use tol <- 0.001 to get the same result as with the regex for numbers like 0.9901
discard <- df$long-trunc(df$long*100)/100 < tol
df[!discard, , drop=FALSE]
# long
# 3 7.41139
# 6 14.48333
# 7 56.99900
You have to modify your regular expression slightly. The following one select all values with three non-zero numbers after the decimal point:
new.df <- df[grep("\\.[1-9][1-9][1-9]", df$long), ]

An elegant way to count number of negative elements in a vector?

I have a data vector with 1024 values and need to count the number of negative entries. Is there an elegant way to do this without looping and checking if an element is <0 and incrementing a counter?
You want to read 'An Introduction to R'. Your answer here is simply
sum( x < 0 )
which works thanks to vectorisation. The x < 0 expression returns a vector of booleans over which sum() can operate (by converting the booleans to standard 0/1 values).
There is a good answer to this question from Steve Lianoglou How to identify the rows in my dataframe with a negative value in any column?
Let me just replicate his code with one small addition (4th point).
Imagine you had a data.frame like this:
df <- data.frame(a = 1:10, b = c(1:3,-4, 5:10), c = c(-1, 2:10))
This will return you a boolean vector of which rows have negative values:
has.neg <- apply(df, 1, function(row) any(row < 0))
Here are the indexes for negative numbers:
which(has.neg)
Here is a count of elements with negative numbers:
length(which(has.neg))
The above solutions prescribed need to be tweaked in-order to apply this for a df.
The below command helps get the count of negative or any other symbolic logical relationship.
Suppose you have a dataframe:
df <- data.frame(x=c(2,5,-10,NA,7), y=c(81,-1001,-1,NA,-991))
In-order to get count of negative records in x:
nrow(df[df$x<0,])

Resources