Home > database >  Use dataframe value as code or arguments in R
Use dataframe value as code or arguments in R

Time:03-17

I have two dataframe, looking like this :

data_smmry <- data.frame(
  var_names=c("weather", "temperature", "date")
)

data_1 <- data.frame(
  date=c(1, 5, 15, 29),
  weather=c("sunny", "sunny", "cloudy", "windy"),
  temperature=c(25, 27, 20, 17)
)

I want to be able to add a "numeric" column to data_smmry, if the corresponding variable in data_1 is numeric or not, based on values in var_names.

This is what I tried so far :

is.numeric(data_1$temperature) return TRUE

data_smmry[2,1] return "temperature"

But what I want is : is.numeric(data_1$data_smmry[2,1]) to also return TRUE, but it currently returns FALSE

How can I use output of data_smmry[2,1] as code for is.numeric(data_1$XXXX) ?

CodePudding user response:

Apply is.numeric to the columns of data_1, then select the elements of this corresponding to data_summry$var_names.

> data_smmry$numeric <- sapply(data_1, is.numeric)[data_smmry$var_names]
> data_smmry
    var_names numeric
1     weather   FALSE
2 temperature    TRUE
3        date    TRUE

If instead you want a variable with the class, then replace is.numeric with class:

data_smmry$type <- sapply(data_1, class)[data_smmry$var_names]

CodePudding user response:

Use [[ instead of $:

is.numeric(data_1[[data_smmry[2,1]]])
# [1] TRUE

data_1$data_smmry[2,1] tries to find a column called "data_smmry" in data_1, which isn’t what you want. Whereas data_1[[data_smmry[2,1]]] first evaluates data_smmry[2,1], which returns "temperature", and is then equivalent to data_1[["temperature"]].

  •  Tags:  
  • r
  • Related