循环遍历列并在r

时间:2018-06-06 15:51:38

标签: r loops

我有一个如下所示的数据框。我想在过去三个月的列中添加最后一个值。在这种情况下,它将是6 + 7 + 0 = 13.如何编写for循环以在R中添加数字

df <- read.table(textConnection('
mth     n value    
jan     1 5      
feb     2 1  
feb     2 3   
mar     3 2  
mar     3 8  
mar     3 0  
apr     4 6  
apr     4 0  
apr     4 2  
apr     4 7  
may     5 2  
may     5 1  
may     5 4  
may     5 2  
may     5 6  
'), header=T)

4 个答案:

答案 0 :(得分:0)

tapply将mth视为字符串,除非它被转换为因子。硬编码月的另一种方法,仅使用3,更具可读性:

df$mth <- factor(df$mth, levels=c('jan', 'feb', 'mar', 'apr', 'may'))
sum(tail(tapply(df$value, df$mth, tail, 1), 3))

另一种方法

sum(tapply(df$value, df$mth, tail, 1)[c('mar', 'apr', 'may')])

答案 1 :(得分:0)

我要回答

  • 使用data.table
  • 由于数据最初出现,如果数据有希望可以采用。不同

<强>解决方案

library(data.table)
dt[, value[.N], mth][(.N-2):.N, sum(V1)]
[1] 13

数据

dt <- structure(list(mth = c("jan", "feb", "feb", "mar", "mar", "mar", 
                       "apr", "apr", "apr", "apr", "may", "may", "may", "may", "may"
), n = c(1L, 2L, 2L, 3L, 3L, 3L, 4L, 4L, 4L, 4L, 5L, 5L, 5L, 
         5L, 5L), value = c(5L, 1L, 3L, 2L, 8L, 0L, 6L, 0L, 2L, 7L, 2L, 
                            1L, 4L, 2L, 6L)), row.names = c(NA, -15L), class = c("data.table", 
                                                                                 "data.frame"))

答案 2 :(得分:0)

使用 tidyverse

library(tidyverse)

df %>% 
    arrange(n) %>% 
    group_by(mth) %>%
    slice(n()) %>% 
    arrange(n) %>% 
    tail(3) %>% 
    ungroup(mth) %>% 
    summarise(sum_value = sum(value))

答案 3 :(得分:0)

使用dplyr的选项可以是:

库(dplyr)

df %>% group_by(n,  mth) %>%
  summarise(last_value = last(value)) %>%
  tail(3) %>%
  ungroup() %>%
  summarise(sum = sum(last_value))

# # A tibble: 1 x 1
# sum
# <int>
#   1    13