重新整形和连接数据帧时出错

时间:2017-09-13 19:53:16

标签: r dataframe reshape

我有一个数据框:

  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")

AND

reshape(costs, direction = "long")

但这会返回错误:

  

重塑时出错(费用,方向=“长”):没有'重塑'   属性,必须指定'变化'

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

2 个答案:

答案 0 :(得分:2)

tidyr包中的聚集功能可以解决问题。

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命令。

答案 1 :(得分:1)

我希望这可以帮到你(仅限R基础)

#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