Reputation: 182
I have two columns in a data frame that represent addresses. I need to melt the two and recast as a single column and am having trouble wrapping my head around reshape2. Example:
A B
123 address st 122 address st
125 address st 124 address st
127 address st 126 address st
and I need this as the output
C
122 address st
123 address st
125 address st
124 address st
127 address st
126 address st
edit: Let me be a little more specific: order does not matter and there are a great deal of columns in the data frame I am working within, not just the two that need to be merged. It would be great if a new, seperate data frame could be created that is a single column with all the data from address column 1 (A) and address column 2 (B). The addresses (obviously) need to be preserved in their current form. Some answers given have been mashing the data together in a way that does solve this issue.
Upvotes: 2
Views: 149
Reputation: 1719
Melting and recasting will also preserve which column the original data came from. This is useful if you still need that information, but not as a seperate column. For example:
library(reshape2)
A <- c('123 address st', '125 address st', '127 address st')
B <- c('122 address st', '124 address st', '126 address st')
DF <- data.frame(A, B, stringsAsFactors = FALSE)
DF2 <- melt(data = DF, value.name = 'C', measure.vars = c('A', 'B'))
Will return
> DF
A B
1 123 address st 122 address st
2 125 address st 124 address st
3 127 address st 126 address st
> DF2
variable C
1 A 123 address st
2 A 125 address st
3 A 127 address st
4 B 122 address st
5 B 124 address st
6 B 126 address st
If you need it sorted, you can use the dplyr pacakge for that rather easily:
library(dplyr)
arrange(DF2, C)
Returns:
variable C
1 B 122 address st
2 A 123 address st
3 B 124 address st
4 A 125 address st
5 B 126 address st
6 A 127 address st
And if you want to remove that pesky variable
column, you can also use dplyer. Using it's magrittr
-based piping functionality:
> DF3 <- select(DF2, C) %>% arrange(C)
> DF3
C
1 122 address st
2 123 address st
3 124 address st
4 125 address st
5 126 address st
6 127 address st
Upvotes: 1
Reputation: 1717
It may be easier than you think. Do you need to stack the columns?
df2 = data.frame(C=rbind(df$A, df$B))
Upvotes: 0
Reputation: 887501
You can try
data.frame(C=c(t(df)), stringsAsFactors=FALSE)
# C
#1 123 address st
#2 122 address st
#3 125 address st
#4 124 address st
#5 127 address st
#6 126 address st
Upvotes: 2