increasing value by one with each occurrence of non-repeated number [duplicate] - r

This question already has answers here:
Increment by 1 for every change in column
(6 answers)
Closed 2 years ago.
v <- c(1,1,2,3,3,3,1,1,3,4,4)
I'm trying to create a vector of elements in which the first occurrence of a non-repeated number always increases by one relative to the previous number.
This is the desired output
1,1,2,3,3,3,4,4,5,6,6
What would an efficient way of doing this would be?

A base R option with rle
> with(rle(v),rep(seq_along(values),lengths))
[1] 1 1 2 3 3 3 4 4 5 6 6
or data.table::rleid
> data.table::rleidv(v)
[1] 1 1 2 3 3 3 4 4 5 6 6

Related

How to create an vector with incremental values based on values of another vector? [duplicate]

This question already has answers here:
Generate an incrementally increasing sequence like 112123123412345
(4 answers)
Closed 1 year ago.
I have a vector v1 and using it, I want to create another vector v2
Here, v1 = c(7,6,5), v2 = c(1,2,3,4,5,6,7,1,2,3,4,5,6,1,2,3,4,5)
I want to get v2 with and without loops, both. How is it done?
You can use sequence to generate the numbers.
sequence(v1)
# [1] 1 2 3 4 5 6 7 1 2 3 4 5 6 1 2 3 4 5
And with a loop using lapply:
unlist(lapply(v1, seq))

Go through a column and collect a running total in new column [duplicate]

This question already has answers here:
Creation of a specific vector without loop or recursion in R
(2 answers)
Split data.frame by value
(2 answers)
Closed 4 years ago.
I have a dataframe whose rows represent people. For a given family, the first row has the value 1 in the column A, and all following rows contain members of the same family until another row in in column A has the value 1. Then, a new family starts.
I would like to assign IDs to all families in my dataset. In other words, I would like to take:
A
1
2
3
1
3
3
1
4
And turn it into:
A family_id
1 1
2 1
3 1
1 2
3 2
3 2
1 3
4 3
I'm playing with a dataframe of 3 million rows, so a simple for-loop solution I came up with falls short of necessary efficiency. Also, the family_id need not be sequential.
I'll take a dplyr solution.
data:
df <- data.frame(A = c(1:3,1,3,3,1,4))
code:
df$familiy_id <- cumsum(c(-1,diff(df$A)) < 0)
result:
# A familiy_id
#1 1 1
#2 2 1
#3 3 1
#4 1 2
#5 3 2
#6 3 2
#7 1 3
#8 4 3
please note:
This solution starts a new group when a number occurs that is smaller than the previous one.
When its 100% sure that a new group always begins with a 1 consistently, then ronak's solution is perfect.

How to assign IDs for consecutive rows in R split by a given kind of row? [duplicate]

This question already has answers here:
Creation of a specific vector without loop or recursion in R
(2 answers)
Split data.frame by value
(2 answers)
Closed 4 years ago.
I have a dataframe whose rows represent people. For a given family, the first row has the value 1 in the column A, and all following rows contain members of the same family until another row in in column A has the value 1. Then, a new family starts.
I would like to assign IDs to all families in my dataset. In other words, I would like to take:
A
1
2
3
1
3
3
1
4
And turn it into:
A family_id
1 1
2 1
3 1
1 2
3 2
3 2
1 3
4 3
I'm playing with a dataframe of 3 million rows, so a simple for-loop solution I came up with falls short of necessary efficiency. Also, the family_id need not be sequential.
I'll take a dplyr solution.
data:
df <- data.frame(A = c(1:3,1,3,3,1,4))
code:
df$familiy_id <- cumsum(c(-1,diff(df$A)) < 0)
result:
# A familiy_id
#1 1 1
#2 2 1
#3 3 1
#4 1 2
#5 3 2
#6 3 2
#7 1 3
#8 4 3
please note:
This solution starts a new group when a number occurs that is smaller than the previous one.
When its 100% sure that a new group always begins with a 1 consistently, then ronak's solution is perfect.

Missing values in sequence into actual sequence in R? [duplicate]

This question already has answers here:
How to create a consecutive group number
(13 answers)
Closed 5 years ago.
I have a vector of integers, for example, v <- c(1,5,1,2,2,4,7,5,7). If I sort(unique(v)), the values 3 and 6 would be missing in the sequence. How can I transform v into a vector where sort(unique(v)) is an actual sequence of integers? This is, transforming v into c(1,4,1,2,2,3,5,3,5) (in general, of course).
Converting v to factor and back to numeric could do the trick
as.numeric(as.factor(v))
#[1] 1 4 1 2 2 3 5 4 5
Using OP's method, we get the expected output with match
match(v, sort(unique(v)))
#[1] 1 4 1 2 2 3 5 4 5

Tallying values in single column and separating into Rows in R [duplicate]

This question already has answers here:
Counting the number of elements with the values of x in a vector
(20 answers)
Closed 6 years ago.
I have a single row of numbers. I'm wondering how I can separate it out so that it outputs columns that total the tally of each set of numbers. I've tried playing around with "separate" but I can't figure out how to make it work.
Here's my data frame:
2
2
2
2
2
4
4
4
I'd like it to be
2 4
5 3
You can use the table() function.
> df
V1
1 2
2 2
3 2
4 2
5 2
6 4
7 4
8 4
> table(df$V1)
2 4
5 3
We can use tabulate which would be faster
tabulate(factor(df1$V1))
#[1] 5 3

Resources