Produce a sequence count by factor in R [duplicate] - r

This question already has answers here:
Create a sequential number (counter) for rows within each group of a dataframe [duplicate]
(6 answers)
Create numbered sequence for occurrences of a given nesting variable
(2 answers)
Closed 7 years ago.
I have simple dataset, one row with factors - I'd like to create a second column (SEQ) and count/sequence the factor LO column
LO SEQ
a 1
a 2
a 3
b 1
b 2
I want to count the LO factors like so...i.e. I want to create the SEQ column.
Looks so easy - but I'm stuck.

We can use getanID
library(splitstackshape)
getanID(df1, 'LO')[]
# LO .id
#1: a 1
#2: a 2
#3: a 3
#4: b 1
#5: b 2

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

Is there a way in R to make all possible combinations between rows of different columns? [duplicate]

This question already has answers here:
Unique combination of all elements from two (or more) vectors
(6 answers)
Generate list of all possible combinations of elements of vector
(10 answers)
Closed 2 years ago.
I have a df with one column and I would like to make combinations with the values of this column in order to have a new df with two columns, like he simple example below: (Obs: my df has ~5000 rows)
df
CG
1
2
3
##I would like a result similar to this:
> head(df1)
C1 C2
1 1
1 2
1 3
2 1
2 2
2 3
3 1
3 2
3 3
Does someone could help me?
Thank you in advance

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

Row numbering by group and date [duplicate]

This question already has answers here:
Numbering rows within groups in a data frame
(10 answers)
numbering by groups [duplicate]
(8 answers)
Closed 6 years ago.
I have a question about numbering rows by group AND by one further condition. I know how to do this by group but not by adding one further condition.
Suppose I have the ID and the DATE and want to create NUM as shown in the table:
ID ...... DATE...... NUM
1 20160103 ...... 1
1 20160104...... 1
1 20160104...... 2
1 20160105...... 1
1 20160105...... 2
1 20160105...... 3
1 20160106...... 1
2 20160103...... 1
2 20160103...... 2
2 20160105...... 1
Any one knows How to do this?
We can use ave from base R
df$NUM <- with(df, ave(ID, ID, DATE, FUN =seq_along))

Use a 'for' loop for the 'aggregate' command? [duplicate]

This question already has answers here:
Mean per group in a data.frame [duplicate]
(8 answers)
Closed 7 years ago.
I've got a data frame as so,
Treatment Type Numerical Value
1 A 3
1 B 2
1 A 8
1 B 7
2 B 4
2 B 1
2 A 2
2 A 2
I want to make a table of means for each type and treatments.
Using aggregate, I have: aggregate(df[,3], list(Treatment) ,mean) which gives me the means for each treatment but not separated by type too. I was thinking this could be rectified by a for-loop.
Note: This is just a subset of the data, and the list of numerical values is hundreds for each type and treatment.
Since I don't have repu to comment:
aggregate(df, list(Treatment,Type), mean)

Resources