停止dplyr在gather(...)%<%separate(...)中拆分名称

时间:2017-12-19 02:19:44

标签: r dplyr

如何在执行gather separate时停止dplyr自动拆分名称?

cost <- data.frame(Date = rep('1970-01-01', 3), 
                   Atr = runif(3), 
                   Atrb = runif(3), 
                   a_t = runif(3), 
                   b_t = runif(3), 
                   a_t = runif(3), 
                   b_t = runif(3), 
                   a_t = runif(3), 
                   b_t = runif(3))

names(cost) <- c('Date', 'Atr','Atrb','a_t','b_t','a_t','b_t','a_t','b_t')

# Now we reshape and see the problem
idx = 4:9 
names(cost)[idx] = paste0(names(cost)[idx], ".", c(sapply(LETTERS[1:3], rep, times = 2)) )

cost.long = cost %>%
    gather(key, value, -c(Date:Atrb)) %>%
    separate(key, into = c("Metric", "Group"))

cost.long

您将在输出中注意到变量名称已被拆分,现在只有一个组t

> cost.long
         Date       Atr      Atrb Metric Group     value
1  1970-01-01 0.3555041 0.9343369      a     t 0.9022378
2  1970-01-01 0.2812809 0.9770834      a     t 0.0204206

1 个答案:

答案 0 :(得分:2)

我们可以在public class Test { public Test() { DataStream<Tuple2<String, JSONObject>> MetaAlert = events .flatMap(new JSONParser()) .keyBy(new KeySelector<Tuple2<String,JSONObject>, String>() { @Override public String getKey(Tuple2<String, JSONObject> json) throws Exception { return json.f0; } }) .timeWindow(Time.seconds(5)) .apply(new GenerateMetaAlert()); } public class GenerateMetaAlert implements WindowFunction<Tuple2<String, JSONObject>, Tuple2<String, JSONObject>, String, TimeWindow> { @Override public void apply(String key, TimeWindow timeWindow, Iterable<Tuple2<String, JSONObject>> iterable, Collector<Tuple2<String, JSONObject>> collector) throws Exception { } } }

中使用sep参数
separate