For example, I have this data frame:
| Id | Age |
|---|---|
| 1 | 14 |
| 2 | 28 |
and I want to make a long column like this:
| Id | new column |
|---|---|
| 1 | 1 |
| 2 | 2 |
| 14 | |
| 28 |
What should I do?
CodePudding user response:
We may unlist data and create the column by padding NA based on the max length
lst1 <- list(df1$id, unlist(df1))
out <- data.frame(lapply(lst1, `length<-`, max(lengths(lst1))))
names(out) <- c("id", "new_column")
CodePudding user response:
Here is another approach:
df1 <- data.frame(New_column = c(df[,"Id"], df[,"Age"]))
merge(df$Id, df1, by="row.names", all=TRUE)[,-1]
Output:
x New_column
1 1 1
2 2 2
3 NA 14
4 NA 28
