jeudi 19 août 2021

R - Filling column of dataframe with values of other columns based on a string in another column

I actually have a pretty easy task to do, but I just cannot find a solution. I have one df with 2 columns of numbers and 1 column of 3 different strings. I want to add now a 4th column V4 that I want to fill with the values of V1 and V2, depending on the V3 column.

> df
   V1 V2 V3
1   1  6  P
2   2  7  P
3   3  8  N
4   4  9  B
5   5 10  P
6   6 11  B
7   7 12  N
8   8 13  N
9   9 14  P
10 10 15  P

structure(list(V1 = 1:10, V2 = 6:15, V3 = c("P", "P", "N", "B", "P", "B", "N", "N", "P", "P")), row.names = c(NA, -10L), class = "data.frame")

For "P" I want to take V1, for "N" I want to take V2 and for "B" I ideally want both values next to each other (V1|V2), but without making them a character, they have to stay numeric. If that is not possible then the higher number should be filled in.

My output should look like this (as numeric). Or if not possible to display 4|9 or something similar as a numeric, then just the nigher number of these 2.

   V1 V2 V3   V4
1   1  6  P    1
2   2  7  P    2
3   3  8  N    8
4   4  9  B  4|9
5   5 10  P    5
6   6 11  B 6|11
7   7 12  N   12
8   8 13  N   13
9   9 14  P    9
10 10 15  P   10

I found a lot how to do this with just filling the column, but I cant find any examples filling the column with values of other columns based on 3 conditions. I tried if-statements with loops and subsets, but I failed so far.

Aucun commentaire:

Enregistrer un commentaire