How do I interact specific values of a dataframe with a function?

Asked

Viewed 39 times

-3

       Idade  AT.49M    AT.83M      AT.2000M
         0    0.00404    0.00269    0.00231
         1    0.00158    0.00105    0.00091
         2    0.00089    0.00059    0.00050
         3    0.00072    0.00048    0.00041
         4    0.00063    0.00042    0.00036

Having this dataframe run the function

px<-function(a) {
  px=1-a
 return(px)
 }

How do I interact a specific df value with the function?

EX: I want the function of idade 2 of AT.83M

1 answer

1

Use indexing to locate values that match certain criteria:

df[df$Idade == 2, "AT.83M"]
# ou
with(df, AT.83M[Idade == 2])

Store the value in an object or apply the function directly:

> px(df[df$Idade == 2, "AT.83M"])
[1] 0.99941

If you are using R, learn indexing. This class from UFPR is a good introduction.

The data used:

df <- read.table(text = c("
  Idade     AT.49M     AT.83M   AT.2000M
      0    0.00404    0.00269    0.00231
      1    0.00158    0.00105    0.00091
      2    0.00089    0.00059    0.00050
      3    0.00072    0.00048    0.00041
      4    0.00063    0.00042    0.00036"),
  header = TRUE)

px <- function(a) 1-a

Browser other questions tagged

You are not signed in. Login or sign up in order to post.