Extract value from data frame?

If I have a data frame, the standard approach to extracting the value is to use a logical / logical expression to match the correct row and column. Example:

set.seed(1) df <- data.frame(letters = letters[1:3], numbers = as.character(c(1, 2, 1)), value=rnorm(3)) subset(df, letters=="c" & numbers=="1")$value [1] -0.8356286 

However, concatenating strings into many == statements seems a bit kludgey. Another way is to use string names as keys:

 Key <- function(...) paste(..., sep="%") # this could be any formatting row.names(df) <- with(df, Key(letters, numbers)) df[Key("c", "1"), "value"] [1] -0.8356286 

In tidyverse, using row names is not recommended - what would be the recommended way to match and retrieve values?

+5
source share

Source: https://habr.com/ru/post/1264866/


All Articles