mercredi 9 septembre 2015

How to change values in a column of a data frame based on conditions in another column?

I would like to have an equivalent of the Excel function "if". It seems basic enough, but I could not find relevant help.

I would like to assess "NA" to specific cells if two following cells in a different columns are not identical. In Excel, the command would be the following (say in C1): if(A1 = A2, B1, "NA"). I then just need to expand it to the rest of the column.

But in R, I am stuck!

Here is an equivalent of my R code so far.

df = data.frame(Type = c("1","2","3","4","4","5"),
                File = c("A","A","B","B","B","C"))
df

To get the following Type of each Type in another column, I found a useful function on StackOverflow that does the job.

# determines the following Type of each Type
shift <- function(x, n){
  c(x[-(seq(n))], rep(6, n))
}

df$TypeFoll <- shift(df$Type, 1)
df

Now, I would like to keep TypeFoll in a specific row when the File for this row is identical to the File on the next row.

Here is what I tried. It failed!

for(i in 1:length(df$File)){
df$TypeFoll2 <- ifelse(df$File[i] == df$File[i+1], df$TypeFoll, "NA")
}

df

In the end, my data frame should look like:

aim = data.frame(Type = c("1","2","3","4","4","5"),
                 File = c("A","A","B","B","B","C"),
                 TypeFoll = c("2","3","4","4","5","6"),
                 TypeFoll2 = c("2","NA","4","4","NA","6"))
aim

Oh, and by the way, if someone would know how to easily put the columns TypeFoll and TypeFoll2 just after the column Type, it would be great!

Thanks in advance

Aucun commentaire:

Enregistrer un commentaire