重塑和连接数据框时出错

Error in reshaping and concatenating 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

我希望这些值中的每一个都是单独的一行。然后我想将以下信息添加为每一行的列:

alphas <- c(.005, .0005, .00005, .005, .0005, .00005) 

thresholds <- c(.0000001, .0000001, .0000001, .0000001, .0000001, .0000001)

iterations <- c(200000, 200000, 200000, 2000000, 2000000, 2000000)

我试图从查看 here 和使用开始:

reshape(costs, idvar = new_cost, direction = "long")

reshape(costs, direction = "long")

但是这个 returns 错误:

Error in reshape(costs, direction = "long") : no 'reshapeWide' attribute, must specify 'varying'

我做错了什么,我该如何解决?

tidyr 包中的 gather 函数可以解决问题。

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)

对于这类问题,tidyr 包是比基础 R 更好的工具,但如果只是将单行更改为列格式,一个简单的解决方案是转置,例如 t(df),然后继续重命名和 cbind 命令。

希望对您有所帮助(仅限R base)

#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