Reputation: 881
I have a data frame:
out.new_cost out1.new_cost out2.new_cost out3.new_cost out4.new_cost out5.new_cost
1 11049.18 11056.08 11948.41 11048.89 11049.18 11056.14
I would like each of these values to be a separate row. I would then like to add the following information as a column for each row:
alphas <- c(.005, .0005, .00005, .005, .0005, .00005)
thresholds <- c(.0000001, .0000001, .0000001, .0000001, .0000001, .0000001)
iterations <- c(200000, 200000, 200000, 2000000, 2000000, 2000000)
I tried to start by looking here and using:
reshape(costs, idvar = new_cost, direction = "long")
AND
reshape(costs, direction = "long")
but this returns the error:
Error in reshape(costs, direction = "long") : no 'reshapeWide' attribute, must specify 'varying'
What am I doing wrong and how can I fix it?
Upvotes: 0
Views: 294
Reputation: 316
I hope this can help you (R base only)
#Create data frame
costs = data.frame(out.new_cost=11049.18,
out1.new_cost=11056.08,
out2.new_cost=11948.41,
out3.new_cost=11048.89,
out4.new_cost=11049.18,
out5.new_cost=11056.14)
#Create variable with colnames
costs.n = colnames(costs)
#Reshape costs to costs.rshp and saving the colnames to times column
costs.rshp = reshape(costs, direction="long", varying=list(costs.n), v.names="new_cost",times=costs.n)
#Set the values of new columns
alphas <- c(.005, .0005, .00005, .005, .0005, .00005)
thresholds <- c(.0000001, .0000001, .0000001, .0000001, .0000001, .0000001)
iterations <- c(200000, 200000, 200000, 2000000, 2000000, 2000000)
#Assign new columns
costs.rshp$alphas = alphas
costs.rshp$thresholds = thresholds
costs.rshp$iterations = iterations
Upvotes: 1
Reputation: 24079
The gather function in the tidyr package will do the trick.
df<-read.table(header=TRUE, text="out.new_cost out1.new_cost out2.new_cost out3.new_cost out4.new_cost out5.new_cost
11049.18 11056.08 11948.41 11048.89 11049.18 11056.14")
alphas <- c(.005, .0005, .00005, .005, .0005, .00005)
thresholds <- c(.0000001, .0000001, .0000001, .0000001, .0000001, .0000001)
iterations <- c(200000, 200000, 200000, 2000000, 2000000, 2000000)
library(tidyr)
df<-gather(df)
#rename first column
names(df)[1]<-"cost"
#remove everything after the .
df$cost<-gsub("\\..*" , "", df$cost )
#add the extra columns
answer<-cbind(df, alphas, thresholds, iterations)
For this type of problem the tidyr package is a better tool than base R, but if the case is simply to change a single row into a column format a simple solution is to transpose such as t(df), then continue with renaming and cbind commands.
Upvotes: 2