dsent
dsent

Reputation: 325

Merge two columns, set value based upon original

I have a data.frame looking like this:

date1                  date2
2015-09-17 03:07:00    2015-09-17 11:53:00
2015-09-17 08:00:00    2015-09-18 11:48:59
2015-09-18 15:58:00    2015-09-22 12:14:00
2015-09-22 12:14:00    2015-09-24 13:58:21

I'd like to combine these two into one column, something like:

dates
2015-09-17 03:07:00    
2015-09-17 11:53:00
2015-09-17 08:00:00    
2015-09-18 11:48:59
2015-09-18 15:58:00    
2015-09-22 12:14:00
2015-09-22 12:14:00    
2015-09-24 13:58:21

Please note that dates (like the last but one and the last but two) can be equal. Now I'd like to add a column 'value'. For every date that has it's origin in date1, the value should be 1, if it's origin is in date2, it should be 2.

Adding a new column is obvious. Merging works fine. I've used:

df <- as.data.frame(df$date1)
df <- data.frame(date1 = c(df$date1, test$date2 ))

That works perfectly fine for the merging of the columns, but how to get the correct value for df$value?

The result should be:

dates                  value
2015-09-17 03:07:00    1
2015-09-17 11:53:00    2
2015-09-17 08:00:00    1
2015-09-18 11:48:59    2
2015-09-18 15:58:00    1
2015-09-22 12:14:00    1
2015-09-22 12:14:00    2   
2015-09-24 13:58:21    1

Upvotes: 0

Views: 46

Answers (3)

Mako212
Mako212

Reputation: 7312

There might be a more clever solution, but I'd just separate each column into its own data frame, add a value column, and then rbind() into a single dates data frame.

df1 <- df$date1
df1$value <- 1

df2 <- df$date2
df2$value <- 2

dates <- rbind(df1,df2)

Upvotes: 0

cmaher
cmaher

Reputation: 5225

Can't you just do something like this?

dates1 <- data.frame(dates = c("2015-09-17 03:07:00",
                          "2015-09-17 08:00:00",
                          "2015-09-18 15:58:00",
                          "2015-09-22 12:14:00"), value = 1)

dates2 <- data.frame(dates = c("2015-09-17 11:53:00",
                          "2015-09-18 11:48:59",
                          "2015-09-22 12:14:00",    
                          "2015-09-24 13:58:21"), value = 2)

# row-bind the two data.frames
df <- rbind(dates1, dates2)

# if "dates" is in a string format, convert to timestamp
df$dates <- strptime(df$dates, format = "%Y-%m-%d %H:%M:%S")

# order by "dates"
df[order(df$dates),]

# result:
                dates value
1 2015-09-17 03:07:00     1
2 2015-09-17 08:00:00     1
5 2015-09-17 11:53:00     2
6 2015-09-18 11:48:59     2
3 2015-09-18 15:58:00     1
4 2015-09-22 12:14:00     1
7 2015-09-22 12:14:00     2
8 2015-09-24 13:58:21     2

Upvotes: 0

Mayur
Mayur

Reputation: 124

I tried to mock your problem. If you are not concerned about time complexity, this is the simplest solution that I can suggest.

a = c(1,3,5)
b = c(2,4,6)

df = data.frame(a, b) 
d1 = c()
d2 = c()

for(counter in 1:length(df$a))
{  
  d1 = c(d1,df$a[counter],df$b[counter]) 
  d2 = c(d2,1,2) 
}

df = data.frame(d1, d2) 
print(df)

Input:

a b

1 2

3 4

5 6

Output:

d1 d2

1 1

2 2

3 1

4 2

5 1

6 2

Upvotes: 1

Related Questions