我有以下兩個數據幀:的R - 兩個數據幀連續比較行並返回一個值
df1 <- data.frame(month=c("1","1","1","1","2","2","2","3","3","3","3","3"),
temp=c("10","15","16","25","13","17","20","5","16","25","30","37"))
df2 <- data.frame(period=c("1","1","1","1","1","1","1","1","2","2","2","2","2","2","3","3","3","3","3","3","3","3","3","3","3","3"),
max_temp=c("9","13","16","18","30","37","38","39","10","15","16","25","30","32","8","10","12","14","16","18","19","25","28","30","35","40"),
group=c("1","1","1","2","2","2","3","3","3","3","4","4","5","5","5","5","5","6","6","6","7","7","7","7","8","8"))
我想:
。連續的每一行,檢查是否
df1
中的month
列中的值與列中的值df2
,即df1$month == df2$period
中的值匹配。如果第1步是不正確的,即
df1$month != df2$period
,然後重複步驟1和df2
下一行中與值比較df1
的值,依此類推,直到df1$month == df2$period
。如果
df1$month == df2$period
,檢查是否在df1
的temp
列中的值小於或等於在max_temp
柱的df2
,即df1$temp <= df$max_temp
。如果
df1$temp <= df$max_temp
,在df2
該行中返回值的group
列,該值增加df1
,在新的一列叫做"new_group"
。如果步驟3不是TRUE,即
df1$temp > df$max_temp
,然後返回到步驟1,並在與df1
下一行df2
比較同一行。
輸出數據幀我想的一個例子是:
df3 <- data.frame(month=c("1","1","1","1","2","2","2","3","3","3","3","3"),
temp=c("10","15","16","25","13","17","20","5","16","25","30","37"),
new_group=c("1","1","1","2","3","4","4","5","6","7","7","8"))
我已經與ifelse
功能玩耍,需要一些幫助或重新方向。謝謝!
你故意讓您的數據字符串? –
數據文件實際上是製表符分隔的文本文件,我使用read.table將其上傳到R中作爲數據幀。作爲一名R新手,我不知道數據是字符串。 – user3201532
圍繞數字的引號告訴你你已經有了字符串。另外,要小心字符串冒充爲因素,你會得到'read.table(.... stringsAsFactors = TRUE)'(這很煩人的是默認值) –