Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/72.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何创建基于R中值来自哪个数据帧的变量?_R_Dataframe_Statistics_Data Manipulation - Fatal编程技术网

如何创建基于R中值来自哪个数据帧的变量?

如何创建基于R中值来自哪个数据帧的变量?,r,dataframe,statistics,data-manipulation,R,Dataframe,Statistics,Data Manipulation,我将按时间升序合并以下两个df,不重复。 我的目标是还要有两个新的变量 df1 time freq 1 1.5 1 2 3.5 1 3 4.5 2 4 5.5 1 5 8.5 2 6 9.5 1 7 10.5 1 8 11.5 1 9 15.5 1 10 16.5 1 11 18.5 1 12 23.5 1 13 26.5 1 df2 t

我将按时间升序合并以下两个df,不重复。 我的目标是还要有两个新的变量

df1
   time   freq                  
1   1.5    1
2   3.5    1
3   4.5    2
4   5.5    1
5   8.5    2
6   9.5    1
7  10.5    1
8  11.5    1
9  15.5    1
10 16.5    1
11 18.5    1
12 23.5    1
13 26.5    1

df2
  time freq
1  0.5    6
2  2.5    2
3  3.5    1
4  6.5    1
5 15.5    1
请帮助我创建两个新列的代码:

  • 其中,如果
    freq
    值对应于
    df1
    中的
    time
    ,则新变量(
    var1
    )将记录相关的
    freq
    值,如果df1不存在这样的
    time
    值,则
    0

  • 其中,如果
    freq
    值对应于
    df2
    中的
    time
    ,则第二个新变量(
    var2
    )将记录
    df2
    中的
    freq
    值,如果
    df2
    中不存在这样的
    time
    值,则记录
    0

  • 所以我会有一个如下的表格:

    time var1 var2
    0.5   0    6
    1.5   1    0
    2.5   0    2
    3.5   1    1
    4.5   2    0
    5.5   1    0
    ...
    

    如果我正确理解了数据帧的外观(通过以下方式创建的内容:)

    然后,您将通过以下方式获得您想要的:

    df_new = data.frame(time = sort(unique(c(df1$time, df2$time))), var1 = sapply(sapply(time, function(x) {df1$freq[df1$time == x]}), function(x) {ifelse(length(x) == 0, 0, x)}), var2 = sapply((sapply(time, function(x) {df2$freq[df2$time == x]})), function(x) {ifelse(length(x) == 0, 0, x)}))
    
    

    希望这能有所帮助,

    如果我正确理解了数据帧的外观(可以通过以下方式创建:)

    然后,您将通过以下方式获得您想要的:

    df_new = data.frame(time = sort(unique(c(df1$time, df2$time))), var1 = sapply(sapply(time, function(x) {df1$freq[df1$time == x]}), function(x) {ifelse(length(x) == 0, 0, x)}), var2 = sapply((sapply(time, function(x) {df2$freq[df2$time == x]})), function(x) {ifelse(length(x) == 0, 0, x)}))
    
    

    希望这有帮助,

    代码库R

    df3 <- merge(x = df1, df2, by.x = 'time', by.y = 'time', all = TRUE, sort = TRUE)
    df3$freq.x[is.na(df3$freq.x)] <- 0
    df3$freq.y[is.na(df3$freq.y)] <- 0
    
    数据

    df1 <- read.table(text = 
    'time   freq                  
    1   1.5    1
    2   3.5    1
    3   4.5    2
    4   5.5    1
    5   8.5    2
    6   9.5    1
    7  10.5    1
    8  11.5    1
    9  15.5    1
    10 16.5    1
    11 18.5    1
    12 23.5    1
    13 26.5    1', header = TRUE, stringsAsFactor = FALSE)
    
    df2 <- read.table(text = 
    'time freq
    1  0.5    6
    2  2.5    2
    3  3.5    1
    4  6.5    1
    5 15.5    1', header = TRUE, stringsAsFactor = FALSE)
    

    df1代码基R

    df3 <- merge(x = df1, df2, by.x = 'time', by.y = 'time', all = TRUE, sort = TRUE)
    df3$freq.x[is.na(df3$freq.x)] <- 0
    df3$freq.y[is.na(df3$freq.y)] <- 0
    
    数据

    df1 <- read.table(text = 
    'time   freq                  
    1   1.5    1
    2   3.5    1
    3   4.5    2
    4   5.5    1
    5   8.5    2
    6   9.5    1
    7  10.5    1
    8  11.5    1
    9  15.5    1
    10 16.5    1
    11 18.5    1
    12 23.5    1
    13 26.5    1', header = TRUE, stringsAsFactor = FALSE)
    
    df2 <- read.table(text = 
    'time freq
    1  0.5    6
    2  2.5    2
    3  3.5    1
    4  6.5    1
    5 15.5    1', header = TRUE, stringsAsFactor = FALSE)
    

    df1使用
    tidyverse
    dplyr
    的更直接的方法:

    library(tidyverse)
    
    df1 <- tibble(time = c(1.5, 3.5, 4.5, 5.5), freq = c(1, 1, 2, 1))
    df2 <- tibble(time = c(0.5, 2.5, 3.5, 6.5), freq = c(6, 2, 1, 1))
    
    full_join(df1, df2, by = "time", suffix = c("_1", "_2")) %>% 
      mutate_all(~ .x %>% replace_na(0)) %>% 
      arrange(time)
    
    # A tibble: 7 x 3
       time freq_1 freq_2
      <dbl>  <dbl>  <dbl>
    1   0.5      0      6
    2   1.5      1      0
    3   2.5      0      2
    4   3.5      1      1
    5   4.5      2      0
    6   5.5      1      0
    7   6.5      0      1
    
    库(tidyverse)
    df1%替换_na(0))%>%
    安排(时间)
    #一个tibble:7x3
    时频1频2
    1   0.5      0      6
    2   1.5      1      0
    3   2.5      0      2
    4   3.5      1      1
    5   4.5      2      0
    6   5.5      1      0
    7   6.5      0      1
    
    使用
    tidyverse
    dplyr
    的更直接的方法:

    library(tidyverse)
    
    df1 <- tibble(time = c(1.5, 3.5, 4.5, 5.5), freq = c(1, 1, 2, 1))
    df2 <- tibble(time = c(0.5, 2.5, 3.5, 6.5), freq = c(6, 2, 1, 1))
    
    full_join(df1, df2, by = "time", suffix = c("_1", "_2")) %>% 
      mutate_all(~ .x %>% replace_na(0)) %>% 
      arrange(time)
    
    # A tibble: 7 x 3
       time freq_1 freq_2
      <dbl>  <dbl>  <dbl>
    1   0.5      0      6
    2   1.5      1      0
    3   2.5      0      2
    4   3.5      1      1
    5   4.5      2      0
    6   5.5      1      0
    7   6.5      0      1
    
    库(tidyverse)
    df1%替换_na(0))%>%
    安排(时间)
    #一个tibble:7x3
    时频1频2
    1   0.5      0      6
    2   1.5      1      0
    3   2.5      0      2
    4   3.5      1      1
    5   4.5      2      0
    6   5.5      1      0
    7   6.5      0      1
    
    能否请您重新格式化您的问题并提供