如下面的df
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>jQuery UI Dialog - Modal message</title>
<link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">
<link rel="stylesheet" href="http://sktcho.com/wp-includes/qp2qp-sktcho/style.css">
<script src="https://code.jquery.com/jquery-1.12.4.js"></script>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
<script>
$( function() {
$( "#dialog-message" ).dialog({
modal: true,
buttons: {
Ok: function() {
$( this ).dialog( "close" );
}
}
});
} );
</script>
</head>
<body>
<div id="dialog-message" title="Quick tutorial">
<div id="dialog" title="Basic dialog">
<p> <center><font size="4" color="black">use two fingers to draw and one finger to move the screen</font></center></p>
<p> <center><font size="4" +/- to zoom in & out</font></center></p>
<p><img border="0" src="http://sktcho.com/wp-includes/qp2qp-sktcho/giphy.gif"></p>
</div>
</body>
</html>
预期输出
我需要将df <- data.frame(
name = rep(c("A", "B", "C"),2),
type = c("10", "10", "10","20", "20", "20"),
val = c(1,2,3,4,5,6)
)
> df
name type val
1 A 10 1
2 B 10 2
3 C 10 3
4 A 20 4
5 B 20 5
6 C 20 6
>
val
的所有记录中的name
添加到C
name
的记录的val中,用于相应的A
一个新的type
AC。需要保留name
name
并且没有它的输出。
OUTPUT1
C
OUTPUT2
name type val
1 A 10 1
2 B 10 2
3 C 10 3
4 AC 10 4
5 A 20 4
6 B 20 5
7 C 20 6
8 AC 20 10
更喜欢基于 name type val
1 AC 10 4
2 B 10 2
4 AC 20 10
5 B 20 5
>
的解决方案
答案 0 :(得分:3)
这是一种方式,
shared_ptr
然后获得其他输出,
library(dplyr)
df %>%
mutate(new = as.integer(name %in% c('A', 'C'))) %>%
group_by(type, new) %>%
summarise(name = paste0(name, collapse = ''), val = sum(val)) %>%
ungroup() %>%
select(-new)
# A tibble: 4 × 3
# type name val
# <fctr> <chr> <dbl>
#1 10 B 2
#2 10 AC 4
#3 20 B 5
#4 20 AC 10
答案 1 :(得分:1)
这是另一个(需要tidyr
以及dplyr
)
df1 <- df %>% group_by(type) %>%
summarise(AC=sum(val[name %in% c("A","C")]),B=val[name=="B"]) %>%
gather(key=name,value=val,-type) %>%
arrange(type)
答案 2 :(得分:1)
以下是使用int input_number(const char* question)
{
int k = 0;
char buffer[100];
printf("%s", question);
fgets(buffer, sizeof buffer, stdin);
sscanf(buffer, "%d", &k);
return(k);
}
data.table
或library(data.table)
rbindlist(list(df, setDT(df)[, .(name = "AC", val = sum(val[as.character(name) %chin%
c("A", "C")])) , .(type)][, names(df), with = FALSE]))[order(type, name)]
# name type val
#1: A 10 1
#2: B 10 2
#3: C 10 3
#4: AC 10 4
#5: A 20 4
#6: B 20 5
#7: C 20 6
#8: AC 20 10
dplyr