Cleaning data using R.
Below are three columns (ID, s1, s2) with two missing s1.
If there is a missing s1, I would like to create a column that replaces the same row of s2 with NA. Could someone tell me?
ID s1s2
1 2 1
23 1
3 2 3
4NA2 ← I want to put the NA of s1 here
5NA2 ← I want to put the NA of s1 here
dplyr
.
suppressMessages (library(dplyr))
df<-data.frame(
ID=1:5, s1=c(2,3,2,NA,NA), s2=c(1,1,3,2,2)
)
df<-df%>%mute(s2=replace(s2,is.na(s1),NA))
df
#
ID s1s2
1 1 2 1
2 2 3 1
3 3 2 3
44 NA
55 NA
© 2024 OneMinuteCode. All rights reserved.