Extract two different vectors from set in R - r

How can I extract the set of (a) and set of (b) as shown in the image to two different vectors, the first vector for (a) values and the second for (b) values

Using names
> tmp=c("a"=1,"b"=2,"a"=3,"b"=5)
> tmp[names(tmp)=="a"]
a a
1 3

Related

Ways to check if all sublists have the same length/number of elements?

The main list can be dynamically created (i.e., the number of sublists may vary) so I cannot use all.equal or simply compare the lengths.
In this attached photo for example, you can see that I have three sublists. How can I check if they have the same number of characters or list length(which in this case they do not)?
One way is to use lengths and check if the number of distinct elements are equal to 1 or not
dplyr::n_distinct(lengths(main_list)) == 1
n_distinct will be just length(unique( in base R
length(unique(lengths(main_list))) == 1

What does index do in r?

I have a code I'm working with which has the following line,
data2 <- apply(data1[,-c(1:(index-1))],2,log)
I understand that this creates a new data frame, from the data1, taking column-wise values log-transformed and some columns are eliminated, but I don't understand how the columns are removed. what does 1:(index-1) do exactly?
The ":" operator creates an integer sequence. Because (1:(index-1) ) is numeric and being used in the second position for the extraction operator"[" applied to a dataframe, it is is referring to column numbers. The person writing the code didn't need the c-function. It could have been more economically written:
data1[,-(1:(index-1))]
# but the outer "("...")"'s are needed so it starts at 1 rather than -1
So it removes the first index-1 columns from the object passed to apply. (As MrFlick points out, index must have been defined before this gets passed to R. There's not default value or interpretation for index in R.
Suppose the index is 5, then index -1 returns 4 so the sequence will be from 1 to 4 i.e. and then we use - implies loop over the columns other than the first 4 columns as MARGIN = 2

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))

Remove single value from vector leaving other occurrences of the same value

Suppose I have a large vector of integers in which a single integer can occur in the vector multiple times. I do not know the order of the values within the vector. Consider the code below: I have vector and I want to remove a single 1 to get newVector. Since the order within the vector is not known outside this example, I cannot simply use vector[-1].
vector<-c(1,1,2,2,3)
newVector<-c(1,2,2,3)
Some background: I iteratively pick two values from the vector (using sample) and then want to remove the values I picked from the vector.
Of course I could loop through the vector until I find the first occurrence of the value I wish to remove and remove it using the index, however, that is very time consuming. All the other results I found end up removing all occurrences of the value, which I don't want.
I think this would work, as which.max returns the index of the first match and then we can remove them using negative subsetting.
vector[-which.max(vector == 1)]
#[1] 1 2 2 3
Also, match does the same
vector[-match(1, vector)]
#[1] 1 2 2 3
You could use match. This finds the first occurrence of the specified value returning its index
vector<-c(1,1,2,2,3)
vector[-match(1, vector)]
# [1] 1 2 2 3

Creating Vector in R (multiple conditions)

Need to create and print a vector in R that includes the following in this order:
A sequence of integers from 6 to 10 (inclusive)
A twofold repetition of the vector c(2, -5.1, -33)
The value of the sum of 7/42 and 2
a) Then extract the first and last elements of the vector to form another vector
b) Form a third vector from the elements not extracted above
* Use the vectors from (a) and (b) to reconstruct and print the original first vector
That should do it:
a.vec<-c(seq(6,10,1),rep(c(2,-5.1,-33),times=2),(7/42+2))
b.vec<-a.vec[c(1,length(a.vec))]
c.vec<-a.vec[-c(1,length(a.vec))]
a.vec<-c(b.vec[1],c.vec,b.vec[2])

Resources