Row numbering by group and date [duplicate] - r

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

Related

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

how to change my dataframe based on value of a column [duplicate]

This question already has answers here:
Faster ways to calculate frequencies and cast from long to wide
(4 answers)
Closed 3 years ago.
there is a dataframe with two column as below,and i want to change it into a dataframe with 3 column
df <- data.frame(key=c('a','a','a','b','b'),value=c(1,2,2,1,3))
I have tried it in python,that's ok,but in r i have no idea
the expect output should be like
1 2 3
a 1 2 0
b 1 0 1
library(data.table)
dcast(key~value, data=df, fun.aggregate=length)
# key 1 2 3
# 1 a 1 2 0
# 2 b 1 0 1

How to convert 0=..., 1=... columns, into 1 single column [duplicate]

This question already has answers here:
collapse mulitple columns into one column and generate an index variable
(4 answers)
Reshaping data.frame from wide to long format
(8 answers)
Closed 3 years ago.
I have been tasked to tidy up some data and am having issues with trying to transform the data from this format:
id occupation_busdriver occupation_cashier occupation_nurse
1 0 0 1
2 0 1 0
3 1 0 0
my actual dataset is significantly larger, but this is the area in which I am struggling, and therefore an example for this set would be much appreciated.
I have already tried using the gather and select functions
I am looking to have the data in this format:
id occupation
1 nurse
2 cashier
3 busdriver
We can use max.col to get the column index of the max value per row and based on the index, get the column names
data.frame(df1[1], occupation = sub(".*_", "", names(df1))[-1][max.col(df1[-1])])
# id occupation
#1 1 nurse
#2 2 cashier
#3 3 busdriver

Apply a maximum value to whole group [duplicate]

This question already has answers here:
Aggregate a dataframe on a given column and display another column
(8 answers)
Closed 6 years ago.
I have a df like this:
Id count
1 0
1 5
1 7
2 5
2 10
3 2
3 5
3 4
and I want to get the maximum count and apply that to the whole "group" based on ID, like this:
Id count max_count
1 0 7
1 5 7
1 7 7
2 5 10
2 10 10
3 2 5
3 5 5
3 4 5
I've tried pmax, slice etc. I'm generally having trouble working with data that is in interval-specific form; if you could direct me to tools well-suited to that type of data, would really appreciate it!
Figured it out with help from Gavin Simpson here: Aggregate a dataframe on a given column and display another column
maxcount <- aggregate(count ~ Id, data = df, FUN = max)
new_df<-merge(df, maxcount)
Better way:
df$max_count <- with(df, ave(count, Id, FUN = max))

Produce a sequence count by factor in R [duplicate]

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

Resources