for loop - Writing data from one dataframe to a new column of another in R -
this question has answer here:
i have 2 data frames in r. data, frame monthly sales per department in store, looks this:
while averages, frame average sales on months per department, looks this:
what i'd add column data containing average sales (column 3 of averages) each department. whereas have avg column zeroes, i'd contain overall average sales whatever department listed in row. code have now:
for(j in 1:nrow(avgs)){ for(i in 1:nrow(data)){ if(identical(data[i,4], averages[j,1])){ gd[i,10] <- avgs[j,3] } } } after running loop, avg column in data still zeroes, makes me think if(identical(data[i,4], averages[j,1])) evaluating false... why be? how can troubleshoot issue / there better way this?
are looking merge function?
merge(x = data, y = avgs, = "departmentname", all.x=true) 

Comments
Post a Comment