I have a dataframe named df, just as below.
ID HHmm
1 0840
2 0910
3 1050
4 1210
5 1230
I want to add a third column, type, that follows these rules:
for (k in 1 : nrow(df)){
if(df$HHmm >= 0860 & df$HHmm <= 1060){
df[k, 'type'] = "r1"
}else{
df[k, 'type'] = "c"
}
}
but I get the warning:
In if (df$HHmm >= 0860 & df$HHmm <= 1060) { ... :
the condition has length > 1 and only the first element will be used
and I got a undesirable results, just like below.
ID HHmm type
1 0840 r1
2 0910 r1
3 1050 r1
4 1210 r1
5 1230 r1
The desirable resultes should be like below
ID HHmm type
1 0840 c
2 0910 r1
3 1050 r1
4 1210 c
5 1230 c
Can anyone spot what's wrong here? How to compare HHmm time format data in if loop?
CodePudding user response:
You need to define the index k
; e.g. df$HHmm[k]
.
for (k in 1:nrow(df)){
if(df$HHmm[k] >= 0860 & df$HHmm[k] <= 1060){
df[k, 'type'] = "r1"
}else{
df[k, 'type'] = "c"
}
}
CodePudding user response:
You do not need to (and should not) use a loop to do this kind of operation in R. It is slower, more verbose and harder to comprehend.
You just want to create a column called type
with the value "c" and then assign the value "r1" to the subset of the column that meets your condition:
df$type <- "c"
df$type[df$HHmm >= 0860 & df$HHmm <= 1060] <- "r1"
df
# Output
# ID HHmm type
# 1 1 840 c
# 2 2 910 r1
# 3 3 1050 r1
# 4 4 1210 c
# 5 5 1230 c