简体   繁体   中英

Multiple column labels or names

I want to put two rows of labels, or colnames, (either to a data frame or a matrix), like so:

  Big Big Medium Medium Low Low
  Wet Dry    Wet    Dry Wet Dry
1   6   4      1      3   2   5
2   4   4      1      3   1   6
3   4   3      1      3   1   6
4   5   2      1      2   1   4
5   3   4      1      2   3   5

and be able to refer to a given column by using the two labels, for example column "Medium","Dry".

What you ask for doesn't exist. Names are always character vectors so they are always 1-dimensional. So either you use simply names like "Big Wet" in data frames/matrices or you have to use another data structure such as the a 3-dimensional array with the dimensions size , wet/dry , row where you can name them separately:

> array(c(6,4,4,5,3,4,4,3,2,4,1,1,1,1,1,3,3,3,2,2,2,1,1,1,3,5,6,6,4,5),
        c(5,2,3), list(1:5, c("Wet", "Dry"), c("Big", "Medium", "Low")))
, , Big

  Wet Dry
1   6   4
2   4   4
3   4   3
4   5   2
5   3   4

, , Medium

  Wet Dry
1   1   3
2   1   3
3   1   3
4   1   2
5   1   2

, , Low

  Wet Dry
1   2   5
2   1   6
3   1   6
4   1   4
5   3   5

> a[, "Wet",]
  Big Medium Low
1   6      1   2
2   4      1   1
3   4      1   1
4   5      1   1
5   3      1   3

> a[, "Dry", "Medium"]
1 2 3 4 5 
3 3 3 2 2 
text <- "  Big Big Medium Medium Low Low
  Wet Dry    Wet    Dry Wet Dry
1   6   4      1      3   2   5
2   4   4      1      3   1   6
3   4   3      1      3   1   6
4   5   2      1      2   1   4
5   3   4      1      2   3   5"

You would likely read from a file, and use the file parameter rather than the text parameter that I use below:

labs <- read.table(header=FALSE, text=text, nrows=2)  # This will be used to construct the headers.
data <- read.table(header=TRUE, text=text, skip=1)    # Consume a header line so that the row IDs are not interpreted as data.
names(data) <- apply(labs, 2, paste, collapse=',')    # Overwrite the header read above.

> data

  Big,Wet Big,Dry Medium,Wet Medium,Dry Low,Wet Low,Dry
1       6       4          1          3       2       5
2       4       4          1          3       1       6
3       4       3          1          3       1       6
4       5       2          1          2       1       4
5       3       4          1          2       3       5

> data['Big,Wet']
  Big,Wet
1       6
2       4
3       4
4       5
5       3

This gives you what you asked for but is worthless for anything except visualizing:

dat <- read.table(text="1   6   4      1      3   2   5
2   4   4      1      3   1   6
3   4   3      1      3   1   6
4   5   2      1      2   1   4
5   3   4      1      2   3   5", header=FALSE)[, -1]

nms1 <- unlist(strsplit(gsub("\\s+", " ", "Wet Dry    Wet    Dry Wet Dry"), " "))
nms2 <- unlist(strsplit("Big Big Medium Medium Low Low", " "))

dat2 <- rbind(nms1, dat)
colnames(dat2) <- nms2

rownames(dat2) <- c(" ", 1:(nrow(dat2) - 1))
dat2

Yielding:

  Big Big Medium Medium Low Low
  Wet Dry    Wet    Dry Wet Dry
1   6   4      1      3   2   5
2   4   4      1      3   1   6
3   4   3      1      3   1   6
4   5   2      1      2   1   4
5   3   4      1      2   3   5

And the indexing:

indexer <- function(x, y) as.numeric(dat2[-1, colnames(dat2) == x & dat2[1, ] == y])
indexer("Big", "Dry")

This is clunky and I wouldn't go this route. If you want functional go with the other great responses.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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