dimanche 28 juin 2020

If a row matches a criteria do paste in R

Let's imagine you have a dataframe with two columns ID and POSITION. I want to paste some text depending on the ID value.

In Excel I can do this being A as ID and B as POSITION using =IF(A2<10,CONCATENATE("GK00000",A2,".2:",B2,"..",B2+1),CONCATENATE("GK0000",A2,".2:",B2,"..",B2+1)) but I want to add it to my R script line.

I give you a short example of my input data just ilustrative

ID <- c(1,2,3,4,5,6,7,8,9,10,11,12)
POSITION <- c(10,20,30,40,50,60,70,80,90,100,110,120)
df <- cbind(ID,POSITION)

and the result I'm expecting is

CONCAT <- c("GK000001.2:10..11","GK000005.2:50..51","GK000009.2:90..91",
            "GK000010.2:100..101","GK000012.2:120..121")
dfResult <- cbind(ID,POSITION,CONCAT)

Aucun commentaire:

Enregistrer un commentaire