rep() with each equals a vector - r

I have quick question regarding sequence and each:
vect1 <- c(4, 5, 10, 3, 1)
I want replicate with this vector as each such that first number is replicated 4, second 5, third 10, fourth 3, and fifth equal 1.
rep(1:5, each = vect1)
[1] 1 1 1 1 2 2 2 2 3 3 3 3 4 4 4 4 5 5 5 5
Warning message:
In rep(1:5, each = vect1) : first element used of 'each' argument
rep(1:5, each = c(4, 5, 10, 3, 1))
[1] 1 1 1 1 2 2 2 2 3 3 3 3 4 4 4 4 5 5 5 5
Warning message:
In rep(1:5, each = c(4, 5, 10, 3, 1)) :
first element used of 'each' argument
I know this is misuse of each.

rep(1:5, vect1)
If you have questions about how to work functions in R, try
?function
where "function" is whatever function you want to know about. From ?rep you would have read:
'times' A integer vector giving the (non-negative) number of times to repeat
each element if of length length(x), or to repeat the whole vector if
of length 1. Negative or NA values are an error.

Related

How do you efficiently return the order of an increasing index? [duplicate]

This question already has answers here:
Create group names for consecutive values
(4 answers)
Closed 4 years ago.
I have the following index vector:
TestVec = rep(c(6,8,9,11,18), each = 10)
This reads c(6, 6, ..., 6, 8, 8, ..., 8, 9, 9, ..., 9, ...).
I would like to convert this vector into c(1, 1, ..., 1, 2, 2, ..., 2, 3, 3, ..., 3, ...)
Try
I have improvised a quick-and-dirty method, as follows:
sapply(TestVec, function(x) {which(x == unique(TestVec))})
This works fine, but this takes a lot of time in a large dataset.
Is there any efficient way to improve?
match(TestVec, unique(TestVec))
Another option:
as.numeric(as.factor(TestVec))
# [1] 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 3 3 3 3 3 3 3 3 3 3 4 4 4 4 4 4 4 4 4 4 5 5 5 5 5 5 5 5 5 5
Requiring data.table:
rleid(TestVec)
Here is another one,
c(1, cumsum(diff(TestVec) != 0)) + 1

Count the occurrence of one vector's values in another vector including non match values in R

I have 2 vectors:
v1 <- c(1, 2, 3, 4, 1, 3, 5, 6, 4)
v2 <- c(1, 2, 3, 4, 5, 6, 7)
I want to calculate the occurrence of values of v1 in v2. The expected result is:
1 2 3 4 5 6 7
2 1 2 2 1 1 0
I know there is a function can do this:
table(v1[v1 %in% v2])
However, it only list the matched values:
1 2 3 4 5 6
2 1 2 2 1 1
How can I show all the values in v2?
You can do
table(factor(v1, levels=unique(v2)))
# 1 2 3 4 5 6 7
# 2 1 2 2 1 1 0

Fill array in for-loop with sequences of different lengths [duplicate]

This question already has answers here:
Generate a sequence of numbers with repeated intervals
(6 answers)
Closed 5 years ago.
I've got some struggle with a small issue. What I want to get is a dim=1 array to be filled up with help of this for-loop.
Minimal-Example (it's not working!):
Numbers <- seq(1,5)
Result <- array(NA)
for(n in Numbers){
Result[n] <- seq(n,5)
# The Result array should be like this:
# (1, 2, 3, 4, 5, 2, 3, 4, 5, 3, 4, 5, 4, 5, 5)
}
I guess there a two problems:
The Result[n] don't have the same length
The index n in Result[n] is wrong. Actually, it should be dynamic, thus, change with every new n.
Can you guys help me?
Thank you!
We can do this with sapply
unlist(sapply(Numbers, function(x) seq(x, 5)))
#[1] 1 2 3 4 5 2 3 4 5 3 4 5 4 5 5
Or using the for loop
Result <- c()
for(n in Numbers){
Result <- c(Result, seq(n, 5))
}
Result
#[1] 1 2 3 4 5 2 3 4 5 3 4 5 4 5 5
Using sequence and rep:
n <- 5
sequence(n:1) + rep(0:(n-1), n:1)
# [1] 1 2 3 4 5 2 3 4 5 3 4 5 4 5 5
You may also create an 'oversized' matrix and select the lower triangle:
m <- matrix(c(NA, 1:n), nrow = n + 1, ncol = n + 1)
m[lower.tri(m)]
# [1] 1 2 3 4 5 2 3 4 5 3 4 5 4 5 5

How to count entries with specific values in R [duplicate]

This question already has an answer here:
Include levels of zero count in result of table()
(1 answer)
Closed 6 years ago.
Let's say I have two vectors
x <- c(1, 2, 2, 3, 4, 4, 5, 5, 5)
y <- c(3, 3, 3, 4, 5, 6, 6, 7, 7)
The unique numbers among all the numbers in these vectors are 1:7. I know that if I use the table function in R, I can count the number of unique entries in each of the vectors. For example, if I apply the table function to the first vector, I will get
table(x)
#x
# 1 2 3 4 5
# 1 2 1 2 3
Applying it to the second vector, I will get
table(y)
# y
# 3 4 5 6 7
# 3 1 1 2 2
How can I get it to count the number of occurrences of all unique entries in both vectors? For example, I'd like to produce the following results:
1 2 3 4 5 6 7
1 2 1 2 3 0 0
for the first vector and
1 2 3 4 5 6 7
0 0 3 1 1 2 2
First, generate a list of the values you want to get counts for in both vectors
lvl<-unique(c(x,y))
Then explicitly list those values as levels of a factor before doing table
table(factor(x, lvl))
table(factor(y, lvl))
table(factor(x, unique(union(x,y))))
table(factor(y, unique(union(x,y))))

Remove two outliers in multiple regression

we've got a problem with removing two outliers from our dataset. The data is about an experiment with two independent and one dependent variable. We've exercised the multiple regression and analyzed the "Normal Q-Q" plot. It showed us two outliers (10,46). Now we would like to remove those two cases, before rerunning the multiple regression without the outliers.
We've already tried out various commands recommended in several R platforms but unfortunately nothing worked out.
We would be glad, if anyone of you had an idea that would help us solving our problem.
Thank You very much for helping.
Since no data was provided, I fabricated some:
> x <- data.frame(a = c(10, 12, 14, 6, 10, 8, 11, 9), b = c(1, 2, 3, 24, 4, 1, 2, 4),
c = c(2, 1, 3, 6, 3, 4, 2, 48))
> x
a b c
1 10 1 2
2 12 2 1
3 14 3 3
4 6 24 6
5 10 4 3
6 8 1 4
7 11 2 2
8 9 4 48
If the 4th case in column x$b and the 8th case in column x$c are outliers:
> x1 <- x[-c(4, 8), ]
> x1
a b c
1 10 1 2
2 12 2 1
3 14 3 3
5 10 4 3
6 8 1 4
7 11 2 2
Is this what you need?

Resources