处理字符r

时间:2018-11-14 16:26:18

标签: r dplyr data.table

我有一个看起来像这样的数据框:

   Model            Month    Country Price
1  Audi TT          2016-03  NL      187
1  Audi TT          2017-03  NL      220
1  Audi TT          2016-03  DE      190
4  Volkswagen Golf  2016-08  NL      204

现在,我想汇总各个国家/地区的数据,这样我就只能在特定日期对每种模型进行一次观察。为此,我使用:

DT = data.table(test_data)
test_data_total = DT[, lapply(.SD, mean), by = Month]

来自Can dplyr summarise over several variables without listing each one?

但是,这仅在处理数字变量时有效。在我的情况下,Model变量是一个字符,因此这给了我一个错误,而我当时只想打印一次模型名称。所以之后它应该像这样:

Model            Month    Country Price
1  Audi TT          2016-03  NL      avg
1  Audi TT          2017-03  NL      220
4  Volkswagen Golf  2016-08  NL      204

有人知道该怎么做吗?

2 个答案:

答案 0 :(得分:1)

继续 function fn1(callback) { setTimeout(() => { console.log('hello'); }, 5000); callback(); } function fn2() { setTimeout(() => { console.log('goodbye'); }, 3000); } fn1(function() { // callback setTimeout(function() { // timeout so fn2 fires once fn1 delay is finished fn2(); setTimeout(function() { // add your console.log("completed"); or third function // here so it fires right after fn2 }, 3001); // 3000 fn2 timeout + 1ms more so it fires right after }, 2001); // the difference between fn1 timeout - fn2 timeout + 1ms so it fires right after }); ,请尝试:

data.table

数据

library(data.table)
setDT(test_data)[, .(Country = list(Country), Price = mean(Price)),
                 by = list(Model, Month)][]
# output
             Model   Month Country Price
1:         Audi TT 2016-03   NL,DE 188.5
2:         Audi TT 2017-03      NL 220.0
3: Volkswagen Golf 2016-08      NL 204.0

答案 1 :(得分:0)

尝试:

test_data_total <- DT %>% 
  group_by(Model, Month) %>%
  summarise(Country = toString(unique(Country)),
            Price_avg = mean(Price))