I have an issue in adding one column below another column.
i have a data like this :
Heading 1 | Heading 2 | Value |
---|---|---|
12 | 34 | 1 |
99 | 42 | 0 |
and I want that column 2 be below 1:
Heading 1 | value |
---|---|
12 | 1 |
99 | 0 |
34 | 1 |
42 | 0 |
do you have a suggestion? thank you in advance
CodePudding user response:
You could also rbind
the two dataframes together:
library(tidyverse)
df <- data.frame(
list(
heading1=c(12, 99),
heading2=c(34, 42),
value=c(1,0)))
rbind(df %>% select(heading1, value),
df %>% select(heading2, value) %>% rename(heading1 = heading2))
heading1 value
1 12 1
2 99 0
3 34 1
4 42 0
CodePudding user response:
Completely tidyverse
way, to my mind:
library(tidyverse)
df <- data.frame(
list(
heading1=c(12, 99),
heading2=c(34, 42),
value=c(1,0)))
output <- bind_rows(
df %>% select(heading1, value),
df %>% select(heading1 = heading2, value)
)
CodePudding user response:
You could do
data.frame(Heading = c(df$`Heading 1`, df$`Heading 2`), value = rep(df$Value, 2))
#> Heading value
#> 1 12 1
#> 2 99 0
#> 3 34 1
#> 4 42 0
CodePudding user response:
You can use unlist
and cbind
the result.
cbind("Heading 1" = unlist(df[1:2]), df[3])
# Heading 1 value
#heading11 12 1
#heading12 99 0
#heading21 34 1
#heading22 42 0
Or without the names.
cbind("Heading 1" = unlist(df[1:2], use.names = FALSE), df[3])
# Heading 1 value
#1 12 1
#2 99 0
#3 34 1
#4 42 0