R Melt和dcast基于原始数据帧列的名称
我很难重塑一个数据框以用于错误条形图,将所有列与中心趋势数据相结合,并分别将所有列与错误数据相结合 我从一个数据框开始,其中一列是自变量,然后两列是每个测量的参数:一列是平均值,一列是误差,就像你通常用这种数据格式化电子表格一样。初始数据帧如下所示:R Melt和dcast基于原始数据帧列的名称,r,reshape,R,Reshape,我很难重塑一个数据框以用于错误条形图,将所有列与中心趋势数据相结合,并分别将所有列与错误数据相结合 我从一个数据框开始,其中一列是自变量,然后两列是每个测量的参数:一列是平均值,一列是误差,就像你通常用这种数据格式化电子表格一样。初始数据帧如下所示: df<-data.frame( indep=1:3, Amean=runif(3), Aerr=rnorm(3), Bmean=runif(3), Berr=rnorm(3) ) df.cast<-data
df<-data.frame(
indep=1:3,
Amean=runif(3),
Aerr=rnorm(3),
Bmean=runif(3),
Berr=rnorm(3)
)
df.cast<-data.frame(
indep=rep(1:3, 2),
series=c(rep("A", 3),
rep("B", 3)),
means=runif(6),
errs=rnorm(6)
)
qplot(data=df.cast, x=indep, y=means, ymin=means-errs, ymax=means+errs,
col=series, geom="errorbar")
df.melt<-melt(df, id.vars="indep")
dcast(df.melt,
indep~(variable=="Amean"|variable=="Bmean") + (variable=="Aerr"|variable=="Berr")
)
我一直在尝试熔化,然后使用如下表达式进行重铸:
df<-data.frame(
indep=1:3,
Amean=runif(3),
Aerr=rnorm(3),
Bmean=runif(3),
Berr=rnorm(3)
)
df.cast<-data.frame(
indep=rep(1:3, 2),
series=c(rep("A", 3),
rep("B", 3)),
means=runif(6),
errs=rnorm(6)
)
qplot(data=df.cast, x=indep, y=means, ymin=means-errs, ymax=means+errs,
col=series, geom="errorbar")
df.melt<-melt(df, id.vars="indep")
dcast(df.melt,
indep~(variable=="Amean"|variable=="Bmean") + (variable=="Aerr"|variable=="Berr")
)
df.melt我会这样做:
# Melt the data
mdf <- melt(df, id.vars="indep")
# Separate the series from the statistic with regular expressions
mdf$series <- gsub("([A-Z]).*", "\\1", mdf$variable)
mdf$stat <- gsub("[A-Z](.*)", "\\1", mdf$variable)
# Cast the data (after dropping the original melt variable
cdf <- dcast(mdf[, -2], indep+series ~ stat)
# Plot
qplot(data=cdf, x=indep, y=mean, ymin=mean-err, ymax=mean+err,
colour=series, geom="errorbar")
#融化数据
mdf我会这样做:
# Melt the data
mdf <- melt(df, id.vars="indep")
# Separate the series from the statistic with regular expressions
mdf$series <- gsub("([A-Z]).*", "\\1", mdf$variable)
mdf$stat <- gsub("[A-Z](.*)", "\\1", mdf$variable)
# Cast the data (after dropping the original melt variable
cdf <- dcast(mdf[, -2], indep+series ~ stat)
# Plot
qplot(data=cdf, x=indep, y=mean, ymin=mean-err, ymax=mean+err,
colour=series, geom="errorbar")
#融化数据
mdf您可以使用base R中的重塑
来完成它
df.cast <- reshape(df, varying = 2:5, direction = 'long', timevar = 'series',
v.names = c('mean', 'err'), times = c('A', 'B'))
qplot(data = df.cast, x = indep, y = mean, ymin = mean - err, ymax = mean + err,
colour = series, geom = "errorbar")
df.cast您可以使用底部R中的重塑来完成此操作
df.cast <- reshape(df, varying = 2:5, direction = 'long', timevar = 'series',
v.names = c('mean', 'err'), times = c('A', 'B'))
qplot(data = df.cast, x = indep, y = mean, ymin = mean - err, ymax = mean + err,
colour = series, geom = "errorbar")
工作整形()示例的df.cast+1。除非您确实需要使用名称“time”而不是“series”“要使其正常工作,默认情况下,重塑
使用。可以通过在调用中设置timevar=series
来更改它。我编辑了我的解决方案来反映这一点,这也很有帮助。重塑的帮助页面确实需要重新编写。我花了4、9年的时间才学会如何有效地使用它。我正在制作一套2-3张幻灯片,这套幻灯片将使使用restrape
+1进行有效的restrape()示例变得非常简单(希望如此)。除非您确实需要使用名称“time”而不是“series”“要使其正常工作,默认情况下,重塑
使用。可以通过在调用中设置timevar=series
来更改它。我编辑了我的解决方案来反映这一点,这也很有帮助。重塑的帮助页面确实需要重新编写。我花了4、9年的时间才学会如何有效地使用它。我正在制作一套2-3张幻灯片,这套幻灯片将使使用重塑