I have two data frames df1 and df2:
group=c("Group 1", "Group 2", "Group3","Group 1", "Group 2", "Group3")
year=c("2000","2000","2000", "2015", "2015", "2015")
items=c("12", "10", "15", "5", "10", "7")
df1=data.frame(group, year, items)
year=c("2000", "2015")
items=c("37", "22")
df2=data.frame(year,items)
df1 contains the number of items per year and separated by group, and df2 contains the total number of items per year
I'm trying to create a for loop that will calculate the proportion of items for each group type. I'm trying to do something like:
df1$Prop="" #create empty column called Prop in df1
for(i in 1:nrow(df1)){
df1$Prop[i]=df1$items/df2$items[df2$year==df1$year[i]]
}
where the loop is supposed to get the proportion for each type of item (by getting the value from df1 and dividing by the total in df2) and list it in a new column but this code isn't working.
"in theitemsvector? values are numbers in fact but with your syntax they are converted in factors.