Jenny Pham
Jenny Pham

Reputation: 181

The output from a loop is not correct

I have two tables FDate and Task as follows:

FDate
 Date Cycle Task
1:    1    90    D
2:    2   100    A
3:    3   130    B
4:    3   130    C
5:    4   180 <NA>
6:    5   200    A
7:    5   200    D
8:    6   230 <NA>
Task
 Date Task
1   NA    A
2   NA    B
3   NA    C
4    6    D

I want to write the Task name of same Date from table Task to table FDate. This is the code I try

for (i in 1:nrow(Task)) {
  FDate$Task[FDate$Date %in% Task$Date[i]]<-Task$Task[i]
}

This is the output

 Date Cycle Task
1:    1    90    D
2:    2   100    A
3:    3   130    B
4:    3   130    C
5:    4   180 <NA>
6:    5   200    A
7:    5   200    D
8:    6   230    4

I expect the output is D, not 4. I can't find what is wrong?

Upvotes: 1

Views: 54

Answers (1)

akrun
akrun

Reputation: 886938

The issue is that the column is factor which gets coerced to integer storage mode value. Convert it to character before looping

FDate$Task <- as.character(FDate$Task)
Task$Task <- as.character(Task$Task)

Better, would be to use stringsAsFactors = FALSE either while reading (read.csv/read.table) or if we are creating with data.frame as in both cases, the default option is stringsAsFactors = TRUE and it can create some issues similar to this.


Also, this can be done with a join (assuming the datasets are data.table

library(data.tabl)
FDate[na.omit(df2), Task := i.Task,on = .(Date)]
FDate
#   Date Cycle Task
#1:    1    90    D
#2:    2   100    A
#3:    3   130    B
#4:    3   130    C
#5:    4   180 <NA>
#6:    5   200    A
#7:    5   200    D
#8:    6   230    D

NOTE: changed the second data.table identifier to 'df2' instead of 'Task' as there is a column 'Task' in each dataset

Upvotes: 2

Related Questions