r - 奇怪的 row.names 列出现在 reshape 中

标签 r reshape

我认为我已经很接近解决方案了,我只需要一些正确方向的指示,非常感谢能解决这个问题!

所以我有一个数据集,我只想将其变成两列。这是一个类似的虚拟数据集。

1   1.1  1.2  1.3  1.4
2   2.1  2.2  2.3  2.4
3   3.1  3.2  3.3  3.4
4   4.1  4.2  4.3  4.4

它是一个 csv,所以当我导入它时,R 附加了它自己的标题,如下所示:

V1  V2   V3   V4   V5
1   1.1  1.2  1.3  1.4
2   2.1  2.2  2.3  2.4
3   3.1  3.2  3.3  3.4
4   4.1  4.2  4.3  4.4

我希望它看起来像这样:

id value
1  1.1
1  1.2
1  1.3
1  1.4
2  2.1
2  2.2
...
4  4.4

问题是,这是一个正在进行的项目,数据集(V6、V7 等)会有更多列,所以我无法对任何内容进行硬编码。我保存了一个包含所有标题名称的列表,这似乎有效。

data <- read.csv(file="location", header = FALSE)
dates = ncol(data)
list = 2:dates
variables <-paste0('V',list)

所以现在变量是我想压缩成一个的所有列的列名列表。

我的 reshape 代码是这样的:

newdata <- reshape(data, idvar = "V1", direction = "long", varying = variables, sep="")

但它给了我一个意想不到的数据框。我得到:

row.names   V1  time      V
      1.2    1     2    1.1
      2.2    2     2    2.1
      3.2    3     2    3.1
      4.2    4     2    4.1
      1.3    1     3    1.2
      ...

V1 和 V 列是正确的 - 这是我想要的,并且匹配正确,即使没有排序。但是,row.names 和 time 从何而来?我可以删除时间但不能删除 row.names,因为当我尝试使用 newdata[,1] 访问列 row.names 时,它会给我 V1 列,而 newdata["row.names"] 说“未定义的列已选择”。

因此,如果有人能告诉我我做错了什么或如何重新格式化我的 reshape 语句以便这些奇怪的列不会出现,我将非常感激。谢谢!

最佳答案

您描述的行为...

首先,您描述的行为来自于使用 Viewfix , 如果有 row.names这不仅仅是数据集中行数的序列,在电子表格 View 中,它显示为另一列名为“row.names”的数据。

这是一个小例子:

## Sample data
df1 <- df2 <- data.frame(matrix(1:4, ncol = 2, 
                                dimnames = list(c("A", "B"), c("a", "b"))))
rownames(df2) <- NULL

fix(df1)  # R's spreadsheet view

enter image description here

View(df1) # RStudio data viewer

enter image description here

fix(df2)

enter image description here

View(df2)

enter image description here

获取reshape像你期望的那样工作

二、reshape基 R 中的函数有一个 new.row.names争论。不幸的是,您不能简单地将其设置为 NULL .如果你想摆脱奇怪的row.names,你需要将它设置为一个顺序向量。默认情况下创建的。为此,您需要知道最终数据的长度(不同列数与原始数据集中行数的乘积)。因此,您可以执行以下操作:

id <- "V1"
varCols <- setdiff(names(mydf), "V1")
out <- reshape(mydf, direction = "long", idvar=id, varying=varCols, sep = "", 
               new.row.names=sequence(prod(length(varCols), nrow(mydf))))

这仍然留下 time变量,因此您需要手动删除它,例如:

out$time <- NULL
out
#    V1   V
# 1   1 1.1
# 2   2 2.1
# 3   3 3.1
# 4   4 4.1
# <:::SNIP:::>
# 12  4 4.3
# 13  1 1.4
# 14  2 2.4
# 15  3 3.4
# 16  4 4.4

或者,你可以像你所做的那样,然后设置 row.names(out) <- NULL而不是使用 new.row.names来自 reshape 的论点.

其他方法

在 base R 中,另一种方法是使用 stack并删除“ind”列(这是堆叠数据中的第一列)。然后,只需将其绑定(bind)回“id”列即可。

cbind(mydf[1], stack(mydf[-1])[1])
#    V1 values
# 1   1    1.1
# 2   2    2.1
# 3   3    3.1
# 4   4    4.1
# <:::SNIP:::>
# 12  4    4.3
# 13  1    1.4
# 14  2    2.4
# 15  3    3.4
# 16  4    4.4

或者,正如评论中已经提到的,使用 melt来自“reshape2”:

install.packages("reshape2") ## if it is not yet installed
library(reshape2)
out2 <- melt(mydf, id.vars="V1")
out2$variable <- NULL
out2
#    V1 value
# 1   1   1.1
# 2   2   2.1
# 3   3   3.1
# 4   4   4.1
# <:::SNIP:::>
# 12  4   4.3
# 13  1   1.4
# 14  2   2.4
# 15  3   3.4
# 16  4   4.4

关于r - 奇怪的 row.names 列出现在 reshape 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23177562/

相关文章:

r - 所选索引上每行的平均值

r - 在Rmarkdown文档的ggplot2图表中嵌入字体

r - 带有用户定义链接函数的 glmer 给出错误 : (maxstephalfit) PIRLS step-halvings failed to reduce deviance

r - 使用 ggmap、geom_point 和循环映射长纬度数据集的最近邻居

python - 将列表 reshape 为具有最大行长度的形状

r - 按年份聚合数据并仅当该年份的列中的值时覆盖行中的 NA

r - 在 R 中 - 找到最小数量的单元格创建小于 n 的组

从长到宽 reshape 数据 - 了解 reshape 参数

python - Tensorflow:feed_dict 的形状错误{}

r - 如何 reshape 宽汇总表?