Defining events that happen at the same time as one event [duplicate] - r

This question already has answers here:
Aggregating by unique identifier and concatenating related values into a string [duplicate]
(4 answers)
Closed 5 years ago.
I have a time-series of events:
time <-c("01-01-1970","01-01-1971","01-01-1971","01-01-1972")
event <-c("A","A","B","B")
df <- data.frame(time, event)
time event
1 01-01-1970 A
2 01-01-1971 A
3 01-01-1971 B
4 01-01-1972 B
Now, I would like to put events that happen at the same time in one line. In my example that would be rows 2 and 3. The outcome should look like this:
time event
1 01-01-1970 A
2 01-01-1971 A & B
4 01-01-1972 B
Any ideas how to do this?
Best,
Felix

You can use aggregate:
aggregate(df$event,by=list(df$time),FUN= paste,collapse = " & ")

Related

how to get the day information out of a string [duplicate]

This question already has answers here:
Extracting unique numbers from string in R
(7 answers)
Closed 2 years ago.
I have a date data which have different input format. I would like to keep only number for it. what should I do.
The data looks like this:
The codes are:
Days<-c("Day 1","Day 4"," Day_6", "Day7")
Sample.data <- data.frame(Days)
Basicly I want to get the number out of Days. Was thinking use 'str_replace' or 'gsub' but don't know how to handle different pattern. Please give me as many methods as possible for this problem. Thanks.
Does this work:
> Sample.data$Day <- as.numeric(gsub('(.*)(\\d)', '\\2', Sample.data$Days))
> Sample.data
Days Day
1 Day 1 1
2 Day 4 4
3 Day_6 6
4 Day7 7
>

Create a vector: c(1,2,4,2,2) and use rank variable to return c(1,2,5,2,2) [duplicate]

This question already has answers here:
Extending rank() "Olympic Style"
(2 answers)
R - Add row index to a data frame but handle ties with minimum rank
(3 answers)
Rank with Ties in R
(1 answer)
Closed 2 years ago.
I would like to create a vector:
c(1,2,4,2,2)
And use rank variable to return:
c(1,2,5,2,2)
How do I use rank?
Check out the ?rank help page for options, specifically the ties.method= paramter. Specifically the output you want can be generated with ties.method="min"
rank(c(1,2,4,2,2), ties.method = "min")
# [1] 1 2 5 2 2
Using min_rank from dplyr
library(dplyr)
min_rank(c(1,2,4,2,2))
#[1] 1 2 5 2 2

Subset a dataframe according to very specific conditions [duplicate]

This question already has answers here:
Collapsing data frame by selecting one row per group
(4 answers)
Remove duplicated rows using dplyr
(6 answers)
Closed 6 years ago.
My apologies for this title, i didn't succeeded to find a good explicit title.
Here is a reproducible code for what my data looks like :
subject = gl(3,4,12)
item = factor(c("A","B","B","A","A","A","B","B","A","B","A","B"))
set.seed(123)
rt = runif(12, 1000, 2000)
df = data.frame(subject, item, rt)
> df
subject item rt
1 A 1287.578
1 B 1788.305
1 B 1408.977
1 A 1883.017
2 A 1940.467
2 A 1045.556
2 B 1528.105
2 B 1892.419
3 A 1551.435
3 B 1456.615
3 A 1956.833
3 B 1453.334
I would like to subset my data.frame in order to keep only the first occurence of each item for each subject.
For each subject, the item order is random and each item has been seen twice but i would like to keep only the first occurence.
Any idea of a simple way to do this ?

how to use a vector to call colums from a data frame [duplicate]

This question already has answers here:
Quick question about subsetting via character-class
(3 answers)
Closed 8 years ago.
I have a vector called gas
gas <- c("Hydrogen","Methane")
I also have a data frame called df that looks like
df <- ID Hydrogen Methane
1 2 20
1 3 19
1 2 23
2 8 13
ect..
Normally to use a variable in a data frame I would use df$Hydrogen for example but what I want to know is can I also call Hydrogen by using the vector above? e.g.
data$gas[1]
#In other words I would like the following to be true:
data$gas[1] == data$Hydrogen
what syntax, if any, would I use to obtain this?
Thanks
If you want a specific gas, try:
df[,gas[1]]
For all gases:
df[gas]

Convert columns into multiple rows per entry in R [duplicate]

This question already has answers here:
Convert columns to rows keeping the name of the column
(2 answers)
Closed 9 years ago.
I have the following data:
word Jan-2013 Feb-2013 Mar-2013
A 1 2 3
B 5 2 4
I want to convert the multiple date columns into one, named date and add an additional column for the value.
word date value
A Jan-2013 1
A Feb-2013 2
A Mar-2013 3
B Jan-2013 5
B Feb-2013 2
B Mar-2013 4
Can anyone assist?
Thanks
Additional R options
In addition to Metrics's answer, here are two additional options for R (assuming your data.frame is called "mydf"):
cbind(mydf[1], stack(mydf[-1]))
library(reshape)
melt(mydf, id.vars="word")
Excel option
I am not an Excel user, but since this question is tagged "Excel" as well, I would suggest the Tableau Reshaper Excel add-on.
For your example, it's pretty straightforward:
Go to the "Tableau" menu after installing the add-on and activating it.
Select the cells which contain the values you want to unstack. Click on OK.
View the result.
Using reshape from base R (df1 is your dataframe)
reshape(df1,times=names(df1)[-1],timevar="date",varying=names(df1)[-1],v.names="value",new.row.names=1:6,ids=NULL,direction="long")
word date value
1 A Jan.2013 1
2 B Jan.2013 5
3 A Feb.2013 2
4 B Feb.2013 2
5 A Mar.2013 3
6 B Mar.2013 4

Resources