【问题标题】:forloop with ifelse, merge of two dataset带有if else的for循环,合并两个数据集
【发布时间】:2021-12-21 04:57:18
【问题描述】:

我有两个数据集。 A 有两列: col1 为采样站编号,col2 为日期。 乙: col1 为采样站编号,col2 为日期,col3 为发现物种的年龄,col4 为温度值

例如

A<- matrix(c(1,"2011-07-15",2,"2011-07-20",3,"2011-07-15",4,"2011-07-18",5,"2011-07-20"),ncol=2,byrow=TRUE)
B<-matrix(c(1,"2011-07-12",3, 21, 2,"2011-07-20",4, 20,2,"2011-07-18",2, 20,3,"2011-07-15",10, 21,3,"2011-07-13",8, 22,3,"2011-07-12",7, 19,4,"2011-07-12",9, 19,4,"2011-07-10",7, 21,5,"2011-07-20",8, 21),ncol=4,byrow=TRUE)


>A
   [,1] [,2]        
[1,] "1"  "2011-07-15"
[2,] "2"  "2011-07-20"
[3,] "3"  "2011-07-15"
[4,] "4"  "2011-07-18"
[5,] "5"  "2011-07-20"

> B
      [,1] [,2]         [,3] [,4]
 [1,] "1"  "2011-07-12" "3"  "21"
 [2,] "2"  "2011-07-20" "4"  "20"
 [3,] "2"  "2011-07-18" "2"  "20"
 [4,] "3"  "2011-07-15" "10" "21"
 [5,] "3"  "2011-07-13" "8"  "22"
 [6,] "3"  "2011-07-12" "7"  "19"
 [7,] "4"  "2011-07-12" "9"  "19"
 [8,] "4"  "2011-07-10" "7"  "21"
 [9,] "5"  "2011-07-20" "8"  "21"


我希望在表 A 中,每行都有一个 forloop,如果年龄小于 5 天,则取选项卡 B 的有效温度值,如果年龄大于或等于 5 天,则取该表 B 的平均温度车站。

我认为我可以做一个 forlloop 和一个 ifelse,同时我为表 A 中的 temparatura 创建了一个 NA 列。但我一直坚持如何编写代码。你能帮助我吗?谢谢

for(i in 1:nrow(A)){
  ifelse(B$day<5, , )
}

我期望的输出是这样的新表 A

|col1|col2      |col3|
|1   |2011-07-12|21  |
|2   |2011-07-20|20  |
|3   |2011-07-15|20.5|
|4   |2011-07-12|20  |
|5   |2011-07-20|21  |

【问题讨论】:

  • 欢迎来到 SO!您对预期输出的描述对我来说不是很清楚,因此如果您可以根据所需输入提供您期望的输出数据集,将会很有帮助。
  • 请发布所需的输出(最好使用dput(myoutput)

标签: r for-loop if-statement merge


【解决方案1】:

我建议使用dplyr 来合并、过滤和汇总日期框架。但是,对于age &lt; 5 时的情况应该应用什么函数以及表A 和B 的日期有什么区别,还不太清楚。对于这个例子,我将min 函数用于age &gt;= 5 时的情况。

对于未来最好不要使用矩阵,在处理具有不同类型变量的测量时,即表列的类型不同,但data.frames 或tibbles。基本上,矩阵只允许对所有列使用一种数据类型(characterinteger 或其他),并且很难以一致的方式处理它。

请看下面的代码:

library(tidyverse)
library(lubridate)
A<- matrix(c(1,"2011-07-15",2,"2011-07-20",3,"2011-07-15",4,"2011-07-18",5,"2011-07-20"),ncol=2,byrow=TRUE)
B<-matrix(c(1,"2011-07-12",3, 21, 2,"2011-07-20",4, 20,2,"2011-07-18",2, 20,3,"2011-07-15",10, 21,3,"2011-07-13",8, 22,3,"2011-07-12",7, 19,4,"2011-07-12",9, 19,4,"2011-07-10",7, 21,5,"2011-07-20",8, 21),ncol=4,byrow=TRUE)

a <- as_tibble(A, .name_repair = ~ c("ss_id", "date")) %>%
  mutate(ss_id = as.factor(ss_id),
         date = as_date(date))

b <- as_tibble(B, .name_repair = ~ c("ss_id", "date", "age", "temp")) %>%
  mutate(ss_id = as.factor(ss_id),
         date = as_date(date),
         age = as.integer(age),
         temp = as.numeric(temp))

a %>% left_join(b, by = "ss_id") %>%
  group_by(ss_id, date.x) %>%
  summarise(temp = if_else(age > 5, mean(temp), min(temp))) %>%
  unique()

输出:

`summarise()` has grouped output by 'ss_id', 'date.x'. You can override using the `.groups` argument.
# A tibble: 5 x 3
# Groups:   ss_id, date.x [5]
  ss_id date.x      temp
  <fct> <date>     <dbl>
1 1     2011-07-15  21  
2 2     2011-07-20  20  
3 3     2011-07-15  20.7
4 4     2011-07-18  20  
5 5     2011-07-20  21

【讨论】:

    猜你喜欢
    • 2018-07-08
    • 1970-01-01
    • 2023-03-18
    • 1970-01-01
    • 2012-06-04
    • 1970-01-01
    • 2018-07-08
    • 1970-01-01
    • 2018-03-27
    相关资源
    最近更新 更多