使用for循环在数据框中创建新列以计算R中的值? [英] Create new column in data frame using a for loop to calculate value in R?
问题描述
我有两个数据帧df1和df2:
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包含每年的项目数,并按组分开,而df2包含每年的项目总数
df1 contains the number of items per year and separated by group, and df2 contains the total number of items per year
我正在尝试创建一个for循环,该循环将计算每种组类型的项目比例. 我正在尝试做类似的事情:
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]]
}
该循环应该获取每种类型的项目的比例(通过从df1获取值并将其除以df2中的总数)并在新列中列出它,但是此代码不起作用.
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.
推荐答案
您实际上并不需要df2
,这是使用data.table
且仅使用df1
的简单解决方案(我是assuimg items
是数字列,如果没有,则需要将其转换为一个setDT(df1)[, items := as.numeric(as.character(items))]
)
You don't need df2
really, here's a simple solution using data.table
and only df1
(I'm assuimg items
is numeric column, if not, you''ll need to convert it to one setDT(df1)[, items := as.numeric(as.character(items))]
)
library(data.table)
setDT(df1)[, Prop := items/sum(items), by = year]
df1
# group year items Prop
# 1: Group 1 2000 12 0.3243243
# 2: Group 2 2000 10 0.2702703
# 3: Group3 2000 15 0.4054054
# 4: Group 1 2015 5 0.2272727
# 5: Group 2 2015 10 0.4545455
# 6: Group3 2015 7 0.3181818
另一种方法是,如果您已经拥有df2
,则可以在两者之间加入并计算Prop
(同样,我假设items
在实际数据中为数字)
Another way is if you already have df2
, you can join between the two and calculate Prop
while doing so (again, I'm assuming items
is numeric in real data)
setkey(setDT(df1), year)[df2, Prop := items/i.items]
R的基本替代方案
A base R alternative
with(df1, ave(items, year, FUN = function(x) x/sum(x)))
## [1] 0.3243243 0.2702703 0.4054054 0.2272727 0.4545455 0.3181818
这篇关于使用for循环在数据框中创建新列以计算R中的值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!