0

example of buying price columns

I have been given a dataset that I am attempting to perform logistic regression on. However, to do so, I need to merge some columns in R.

For instance in the carevaluations data set, I am given (BuyingPrice_low, BuyingPrice_medium, BuyingPrice_high, BuyingPrice_vhigh, MaintenancePrice_low MaintenancePrice_medium MaintenancePrice_high MaintenancePrice_vhigh)

How would I combine the columns buying price_low, medium, etc. into one column called "BuyingPrice" with the order and their respective data in each column and the same with the maintenanceprice column?

hrbrmstr
  • 77,368
  • 11
  • 139
  • 205
  • look at `?gather()` in `dplyr` package. – Shree Nov 10 '18 at 12:21
  • Welcome to SO. Pictures are neither code nor data unless the question is about image processing. The R tag has lots of info on how to post good questions https://stackoverflow.com/tags/r/info. There are also lots of examples of good questions. Please try to follow the [patterns of other good questions](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example). – hrbrmstr Nov 10 '18 at 13:12

2 Answers2

0
library(dplyr)

df <- data.frame(Buy_low=rep(c(0,1), 10),
                 Buy_high=rep(c(0,1), 10))

one_column <- df %>% 
  gather(var, value)

head(one_column)
      var value
1 Buy_low     0
2 Buy_low     1
3 Buy_low     0
4 Buy_low     1
5 Buy_low     0
6 Buy_low     1
Aleksandr
  • 1,814
  • 11
  • 19
0

It can be done with stack in base R :

df1 <- data.frame(a=1:3,b=4:6,c=7:9)
stack(df1)
#   values ind
# 1      1   a
# 2      2   a
# 3      3   a
# 4      4   b
# 5      5   b
# 6      6   b
# 7      7   c
# 8      8   c
# 9      9   c
moodymudskipper
  • 46,417
  • 11
  • 121
  • 167