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:

dataframe named data

while averages, frame average sales on months per department, looks this:

dataframe named averages

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

Popular posts from this blog

aws api gateway - SerializationException in posting new Records via Dynamodb Proxy Service in API -

asp.net - Problems sending emails from forum -