简体   繁体   English

如何在R中获取此数据结构?

[英]How to get this data structure in R?

I am trying to find Wanted data structure from the current data structure. 我正在尝试从当前数据结构中查找通缉的数据结构。 I know the schematics of the expected data structure partially. 我部分地了解了预期数据结构的示意图。 The wanted data structure includes one more list(...) and factor class. 所需的数据结构还包括一个list(...)factor类。 Current data structure 当前数据结构

> print(dat.m)

         [,1] [,2]
ave_max  150   61
ave       60    0
lepo      41    0

dat.m <- structure(c(150L, 60L, 41L, 61L, 0L, 0L), .Dim = c(3L, 2L), .Dimnames = list(
    c("ave_max", "ave", "lepo"), NULL))

Wanted data structure 想要的数据结构

> print(dat.m)

     Vars    M1    M2 
1 ave_max   150    61 
2 ave        60     0 
3 lepo       41     0 

I know it is schematically something close to the following where unknown structure(c(...) and row.names = c(...) 我知道它在原理上接近以下未知structure(c(...)row.names = c(...)

structure(list(Vars = structure(c(...), .Label = c("ave_max", 
"ave", "lepo"), class = "factor"), M1 = c(150, 60, 
41), M2 = c(61, 0, 0)), .Names = c("Vars", "ave_max", "ave", 
"lepo"), class = "data.frame", row.names = c(...))

R: 3.4.0 (backports) R:3.4.0(反向移植)
OS: Debian 8.7 操作系统:Debian 8.7

We can use tidyverse 我们可以使用tidyverse

library(tidyverse)
dat.m %>% 
    as.data.frame() %>% 
    rownames_to_column('Vars') %>%
    rename(M1 = V1, M2 = V2)
#     Vars  M1 M2
#1 ave_max 150 61
#2     ave  60  0
#3    lepo  41  0

If we need to use data.table 如果我们需要使用data.table

library(data.table)
setnames(setDT(as.data.frame(dat.m), keep.rownames = TRUE), c('Vars', 'M1', 'M2'))[]

If you don't insist on M1 , M2 , etc. as column names, there is an even shorter data.table solution: 如果您坚持使用M1M2等作为列名,那么可以使用更短的data.table解决方案:

library(data.table)   # CRAN version 1.10.4 used
as.data.table(dat.m, keep.rownames = "Vars")
#      Vars  V1 V2
#1: ave_max 150 61
#2:     ave  60  0
#3:    lepo  41  0

If you do insist on M1 , M2 , etc. as column names and your matrix dat.m has many columns, the columns can be renamed: 如果您确实坚持将M1M2等作为列名,并且矩阵dat.m有很多列,则可以重命名这些列:

DT <- as.data.table(dat.m, keep.rownames = "Vars")
setnames(DT, stringr::str_replace(names(DT), "^V(?=\\d+$)", "M"))
DT
#      Vars  M1 M2
#1: ave_max 150 61
#2:     ave  60  0
#3:    lepo  41  0

The regular expression uses a look-ahead assertion to ensure that only columns starting with V and immediately followed and ended by at least one digit are changed. 正则表达式使用先行断言来确保仅更改以V开头,紧随其后并以至少一位结束的列。 Others like Vars , V , V17b , VV3 aren't touched. 其他V17b ,如VarsVV17bVV3均未触及。


If your matrix has many columns and the purpose of your operation is not just to have nice column headers for printing, you may consider to reshape your data from wide to long form. 如果您的矩阵有很多列,并且操作的目的不仅是要打印出漂亮的列标题,您还可以考虑将数据的格式从宽格式转换为长格式。 The long form is preferred by ggplot for instance. 例如, ggplot首选使用长格式。

DT_long <- melt(as.data.table(dat.m, keep.rownames = "Vars"), id.vars = "Vars")
DT_long
#      Vars variable value
#1: ave_max       V1   150
#2:     ave       V1    60
#3:    lepo       V1    41
#4: ave_max       V2    61
#5:     ave       V2     0
#6:    lepo       V2     0

In long form, it is often easier to manipulate your data, for instance, to rename the columns: 从长格式看,通常更容易操作数据,例如,重命名列:

DT_long[, variable := stringr::str_replace(variable, "^V", "M")]
DT_long
#      Vars variable value
#1: ave_max       M1   150
#2:     ave       M1    60
#3:    lepo       M1    41
#4: ave_max       M2    61
#5:     ave       M2     0
#6:    lepo       M2     0

Finally, you can reshape from long to wide form again 最后,您可以再次从长形变形为宽形

dcast(DT_long, Vars ~ ...)
#      Vars  M1 M2
#1:     ave  60  0
#2: ave_max 150 61
#3:    lepo  41  0

Note that the cast formula recognizes two special variables: . 请注意,强制转换公式可识别两个特殊变量: . and ... . ... . represents no variable; 表示无变量; ... represents all variables not otherwise mentioned in formula . ...表示formula未另外提及的所有变量 (See ?data.table::dcast for details). (有关详细信息,请参见?data.table::dcast )。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM