Strobila
Strobila

Reputation: 317

Compute and Add new Variables to a Data Frame in R with conditions

I have a data frame with multiple variables (Station, Depth, mld, Var1, ..., Varn). I want to create new variables with operations based on some of the data in the table according to Depth and mld, for which Depth <= mld. for example, 1) I want to determine the mean of var1 only for depths <= mld and 2) another variable using a function I created. Both have some conditions in the operation.

Here an example of data: https://file.io/UOOfm1VDGDCy

I tried to do like this because I usually use data.table:

library(data.table)
dt <- data.table(Data.1)

#This is the function to create the final column
newVar = function (Var1Surf,Var3,Var2) {
  if (Var2 == 'D1'){
    E = 5.5
  }
  if (Var2 == 'X3'){
    E = 6.5
  }
  f = Var1Surf/Var3
  nwV = 5 - E * (f/(1-f))*ln(f)}

# here I use data.table operation to create the two columns that I need in the table
#operations with conditions
dt2 <- dt[, .("Var1Surf" = mean(unique(subset(dt,Depth<=mld)[,"Var1"]),na.rm = FALSE),
               "final" = newVar(Var1Surf,Var3,Var2)),
         by=c("Station"),]

I am sure you will find a better solution. In any case, mine is not working, giving this error output:

Error in `[.data.table`(data, , .(Var1Surf = mean(as.numeric(unique(subset(data,  : 
  'list' object cannot be coerced to type 'double'

EDIT: I have just noticed that the link above does not work properly, here the data: https://ufile.io/6obew3ir

Upvotes: 0

Views: 90

Answers (1)

akrun
akrun

Reputation: 887951

The syntax should be

dt[, Var1Surf := mean(unique(Var1[Depth <= mld]), na.rm = TRUE)), by = Station]
dt1[, Final := Vectorize(newVar)(Var1Stuf, Var3, Var2)]

Upvotes: 2

Related Questions