1
votes

I have a data frame in R and I want to set the value based on specific matching of rows and columns from two lists.

Here is the process that I have to follow. I have a data frame, such as the following

   test.var1.t1  test.var2.t1   test.var1.t4   test.var3.t1   test.var3.t7
var1          0             0             0               0              0
var2          0             0             0               0              0
var3          0             0             0               0              0
var4          0             0             0               0              0

I have a function that takes as input the column name and returns a value associated with that column name. For example: some_function("test.var1.t1") may return a value of 10.

I have a list of some row names and a list of column names. I need to go through each row name and column name in the list, match them together, evaluate the function for that matched column and post the value in the data frame in the appropriate cell. For example,

list for row names = "var1" "var2" "var4"

list col names = "test.var1.t1" "test.var2.t1" "test.var1.t4" "test.var3.t1"

var1 should match with "test.var1.t1" and "test.var1.t4" so we get function values for "test.var1.t1" and "test.var1.t4" from some_function(). Say that these values are 10 and 20 respectively.

var2 should match with "test.var2.t1" so get function value for "test.var2.t1". Say that this value is 15.

var4 has no matching column and var3 is not in the row list so this should be left alone in the data frame.

Once this is done, the data frame should be changed to this:

      x.var1.t1   x.var2.t1   x.var1.t4   x.var3.t1   x.var3.t7
var1         10           0          20           0           0
var2          0          15           0           0           0
var3          0           0           0           0           0
var4          0           0           0           0           0

What's the best way to perform this?

1

1 Answers

1
votes

You can do something like this :

nn <- c("var1" ,"var2", "var4")
## I am using a for loop because the side effect is desired here
for(x in nn) {
  ## find the corresponding column names
  col <- grep(x,names(dx),value=TRUE)
  ## if any apply the function 
  if(length(col)>0) dx[x,col] <- get_val(x)
}

Testing it with a dummy function get_val:

get_val <- function(x){
  switch(x,
         var1=c(15,20),
         var2=c(10))
} 

and dx :

dx <- read.table(text=" test.var1.t1  test.var2.t1   test.var1.t4   test.var3.t1   test.var3.t7
var1          0             0             0               0              0
var2          0             0             0               0              0
var3          0             0             0               0              0
var4          0             0             0               0              0",
           stringsAsFactors = FALSE)

you get the expected result :

#      test.var1.t1 test.var2.t1 test.var1.t4 test.var3.t1 test.var3.t7
# var1           15            0           20            0            0
# var2            0           10            0            0            0
# var3            0            0            0            0            0
# var4            0            0            0            0            0