【问题标题】:Create TRUE/FALSE dataframe based on the presence/absence of specific variables根据特定变量的存在/不存在创建 TRUE/FALSE 数据框
【发布时间】:2016-04-19 08:16:05
【问题描述】:

我有一个数据框,其中包含从不同季节采集的样本。我想总结一下哪些站点有不同年份的春季(3-5 月)和秋季(9-11 月)的样本。例如,如果站点 A 有 2007 年春季的样本,则单元格显示为“真”。这是一个示例数据集:

Dates <- data.frame(c(as.Date("2007-9-1"),
                  rep(as.Date("2008-3-1"), times = 3) , 
                  rep(as.Date("2008-9-1"), times = 3)))
Sites <- as.data.frame(as.factor(c("SiteA",rep(c("SiteA","SiteB","SiteC"), 2))))
Values <- data.frame(matrix(sample(0:50, 3.5*2, replace=TRUE), ncol=1))
Dataframe <- cbind(Dates,Sites,Values)
colnames(Dataframe) <- c("date","site","value")

我已经设法根据这些函数在这个数据框中创建了因子“季节”。

Dataframe$Months <- as.numeric(format(Dataframe$date, '%m'))
Dataframe$Season <- cut(Dataframe$Months, 
                    breaks = c(1, 2, 5, 8, 11, 12), 
                    labels = c("Winter", "Spring", "Summer", "Autumn", "Winter"), 
                    right = FALSE)

但我不确定从这里去哪里。这是输出的样子。

A <- rep("TRUE",times = 3)
B <- c("FALSE",rep("TRUE",times = 2))
C <- c("FALSE",rep("TRUE",times = 2))

Output <- as.data.frame(rbind(A,B,C))
colnames(Output) <- c("Autumn.07","Spring.07","Autumn.08") 

【问题讨论】:

    标签: r dataframe


    【解决方案1】:

    这是一个命题:

    Dataframe$Samplings <- interaction(Dataframe$Season, unlist(lapply(strsplit(as.character(Dataframe$date), '-'), function(x) x[[1]]) ))
    
    u1 <- unique(Dataframe$site)
    u2 <- unique(Dataframe$Samplings)
    
    output <- matrix(
      matrix(levels(interaction(u1, u2)), nrow=length(unique(Dataframe$site))) %in% 
        interaction(Dataframe$site,Dataframe$Samplings), 
      nrow=length(unique(Dataframe$site))
    )
    
    colnames(output) <- levels(Dataframe$Samplings)
    rownames(output) <- unique(Dataframe$site)
    output # with all time interactions
    # you can clear it with 
    output[, apply(output, 2, sum) != 0]
    

    【讨论】:

      【解决方案2】:

      使用 reshape2::dcast

      Dataframe$site <- gsub("Site","",Dataframe$site)
      Dataframe$year <- format(Dataframe$date, "%y")
      temp <- reshape2::dcast(Dataframe, site ~ Season + year, length)
      (ans <- apply(data.frame(temp[,2:ncol(temp)], row.names=temp[,1]), 1:2, as.logical))
      

      由于标签重复,您的 Dataframe$Season 会出现警告。你可能想解决这个问题。

      【讨论】:

        【解决方案3】:

        我认为这就是您要寻找的。时间标签与问题中的不完全一样,但我希望它仍然可以理解。

        library(reshape2)
        
        # prepare the input, to have a handy label for the columns
        Dataframe$Year <- as.numeric(format(Dataframe$date, '%Y'))
        Dataframe$TimeLabel <- paste0(Dataframe$Season, '.', Dataframe$Year)
        
        # This is in stages, to make it clear what's happening.
        
        # create a data frame with the right structure, but cells holding NA / numbers
        df1 <- dcast(Dataframe, site ~ TimeLabel)
        
        # turn NA / number into false/true, while ignoring the site column
        df2 <- !is.na(df1[, -1])
        
        # add back the site labels for rows
        df3 <- cbind(as.data.frame(df1$site), df2)
        

        【讨论】:

        • 其实1-3阶段可以合并:df3 &lt;- dcast(Dataframe, site ~ TimeLabel, fun.aggregate = function(x) length(x) &gt; 0)
        猜你喜欢
        • 2019-04-10
        • 1970-01-01
        • 1970-01-01
        • 2016-02-13
        • 2015-09-24
        • 1970-01-01
        • 2018-06-10
        • 1970-01-01
        • 2021-09-29
        相关资源
        最近更新 更多