0

I need to conditionally replace rows in a data frame (x) with rows selected at random from another data frame (y).Some of the rows between the two data frames are the same and so data frame x will contain rows with repeated information. What sort of base r code would I need to achieve this?

I am writing an agent based model in r where rows can be thought of as vectors of attributes pertaining to an agent and columns are attribute types. For agents to transmit their attributes they need to send rows from one data frame (population) to another, but according to conditional learning rules. These rules need to be: conditionally replace values in row n in data frame x if attribute in column 10 for that row is value 1 or more and if probability s is greater than a randomly selected number between 0 and 1. Probability s is itself an adjustable parameter that can take any value from 0 to 1.

I have tried IF function in the code below, but I am new to r and have made a mistake somewhere with it as I get this warning:

"missing value where TRUE/FALSE needed"

I reckon that I have not specified what should happen to a row if the conditions are not satisfied.

I cannot think of an alternative method of achieving my aim.

Note: agent.dat is data frame x and top_ten_percent is data frame y.

s = 0.7
N = nrow(agent.dat)

copy <- runif(N)   #to generate a random probability for each row in agent.dat


    for (i in 1:nrow(agent.dat)){
        if(agent.dat[,10] >= 1 & copy < s){
            agent.dat <- top_ten_percent[sample(nrow(top_ten_percent), 1), ]
        }
    }

The agent.dat data frame should have rows that are replaced with values from rows in the top_ten_percent data frame if the randomly selected value of copy between 0 and 1 for that row is less than the value of parameter s and if the value for that row in column 10 is 1 or more. For each row I need to replace the first 10 columns of agent.dat with the first 10 columns of top_ten_percent (excluding column 11 i.e. copy value).

Assistance with this problem is greatly appreciated.

  • In your for-loop you don't select rows. Probably you need to do that, e.g. `agent.dat[i, 10]` and `agent.dat[i, ]`. Most propably you could also streamline your code using the `ifelse` function. – symbolrush Mar 25 '19 at 09:46

2 Answers2

0

This should fix your errors, assuming your data structure fits your code:

copy <- runif(nrow(agent.dat))

s <- 0.7

for (i in 1:nrow(agent.dat)){
        if(agent.dat[i,10] >= 1 & copy[i] < s){
            agent.dat[i,] <- top_ten_percent[sample(1:nrow(top_ten_percent), 1), ]
        }
    }
LAP
  • 6,330
  • 2
  • 11
  • 24
0

So you just need to change a few things.

You need to get a particular value for copy for each iteration of the for loop (use: copy[i]).

You also need to make the & in the if statement an && (Boolean operators && and ||)

Then you need to replace a particular row (and columns 1 through 10) in agent.dat, instead of the whole thing (agent.dat[i,1:10])

So, the final code should look like:

  copy <- runif(N)

  for (i in 1:nrow(agent.dat)){
    if(agent.dat[,10] >= 1 && copy[i] < s){
      agent.dat[i,1:10] <- top_ten_percent[sample(nrow(top_ten_percent), 1), ]
    }
  }
arranjdavis
  • 297
  • 3
  • 10