输入及其预期输出显示我想复制输入行并更新日期条目。我怎么能这样做?
输入
> aa<- data.frame(a=c(1,11,111),b=c(2,22,222),length=c(3,5,1),date=c(as.Date("28.12.2016",format="%d.%m.%Y"), as.Date("30.12.2016",format="%d.%m.%Y"), as.Date("01.01.2017",format="%d.%m.%Y")))
> aa
a b length date
1 1 2 3 2016-12-28
2 11 22 5 2016-12-30
3 111 222 1 2017-01-01
预期输出
a b length date
1 1 2 3 2016-12-28
2 1 2 3 2016-12-29
3 1 2 3 2016-12-30
4 11 22 5 2016-12-30
5 11 22 5 2016-12-31
6 11 22 5 2017-01-01
7 11 22 5 2017-01-02
8 11 22 5 2017-01-03
9 111 222 1 2017-01-01
答案 0 :(得分:2)
您可以使用base
,dplyr
或data.table
进行分组操作。首先重复行以获得正确的新数据大小。然后增加日期。
library(dplyr)
aa2 <- aa[rep(1:nrow(aa), aa$length),]
aa2 %>% group_by(a,b) %>% mutate(date= date + 1:n() - 1L)
# Source: local data frame [9 x 4]
# Groups: a, b [3]
#
# a b length date
# <dbl> <dbl> <dbl> <date>
# 1 1 2 3 2016-12-28
# 2 1 2 3 2016-12-29
# 3 1 2 3 2016-12-30
# 4 11 22 5 2016-12-30
# 5 11 22 5 2016-12-31
# 6 11 22 5 2017-01-01
# 7 11 22 5 2017-01-02
# 8 11 22 5 2017-01-03
# 9 111 222 1 2017-01-01
#data.table
library(data.table)
aa2 <- aa[rep(1:nrow(aa), aa$length),]
setDT(aa2)[, date := date + 1:.N - 1L, by= .(a,b)]
#base
aa2 <- aa[rep(1:nrow(aa), aa$length),]
transform(aa2, date=ave(date, a, FUN=function(x) x + 1:length(x) - 1L))
为了更简洁的语法,我们可以利用data.table的回收规则,信用@Henrik:
setDT(aa)[ , .(date = date + 1:length - 1), by = .(a, b)]
答案 1 :(得分:0)
不像使用dplyr和data.table包那样优雅,但是低级别:
replicaterow1 <- function(df1 = aa) {
newdf <- df1[0,]
rowss <- nrow(df1)
rowcount <- 1
for (i in 1:rowss) {
rowi <- df1[i,]
reps <- as.integer(rowi[3])
newrow <- rowi
newdf[rowcount,] <- rowi
rowcount <- rowcount + 1
if (reps > 1) {
for(j in 1:(reps-1)) {
newrow[4] <- newrow[4] + 1
newdf[rowcount,] <- newrow
rowcount <- rowcount + 1
}
}
}
return(newdf)
}
答案 2 :(得分:0)
此
<!DOCTYPE html>
<meta charset="utf-8">
<style>
/*set the axis line color, dot stroke, font size, and font position*/
body {
font: 13px helvetica;
}
.name{
position: relative;
top: 90px;
text-align: left;
font-weight: bold;
}
.title {
position: relative;
text-align: left;
font-size: 25px;
}
.axis path,
.axis line {
fill: none;
stroke: #000;
shape-rendering: crispEdges;
}
.dot {
stroke: #000;
}
#filter {
position: absolute;
}
#mark {
padding-left: 150px;
position: inherit;
}
#xAXs {
position: relative;
left: 290px;
bottom: 30px;
}
#yAXs {
position: relative;
bottom: 30px;
left: 315px;
}
#label {
position: absolute;
top: 599px;
bottom: 125px;
left: 300px;
right: 0px;
}
#label2 {
position: absolute;
top: 599px;
bottom: 125px;
left: 430px;
right: 0px;
}
</style>
<body>
<script src="http://d3js.org/d3.v3.min.js"></script>
<script>
var margin = {top: 20, right: 20, bottom: 30, left: 40},
width = 960 - margin.left - margin.right,
height = 500 - margin.top - margin.bottom;
var x = d3.scale.linear()
.range([0, width]);
var y = d3.scale.linear()
.range([height, 0]);
var color = d3.scale.category10();
var axisNames = {
Hour: 'Hour',
Day: 'Day',
};
// define the x scale (horizontal)
var mindate = new Date(2012,0,1),
maxdate = new Date(2012,0,31);
var xScale = d3.time.scale()
.domain([mindate, maxdate]) // values between for month of january
.range([padding, width - padding * 2]); // map these the the chart width = total width minus padding at both sides
var xAxis = d3.svg.axis()
.scale(xScale)
.orient("bottom");
var yAxis = d3.svg.axis()
.scale(y)
.orient("left");
var svg = d3.select("body").append("svg")
.attr("width", width + margin.left + margin.right)
.attr("height", height + margin.top + margin.bottom)
.append("g")
.attr("transform", "translate(" + margin.left + "," + margin.top + ")");
d3.csv("file1.csv", function(error, data) {
data.forEach(function(d) {
d.Day = +d.Day;
d.Hour = +d.Hour;
});
x.domain(d3.extent(data, function(d) { return d.Day; })).nice();
y.domain(d3.extent(data, function(d) { return d.Hour; })).nice();
svg.append("g")
.attr("class", "x axis")
.attr("transform", "translate(0," + height + ")")
.call(xAxis)
.append("text")
.attr("class", "label")
.attr("x", width)
.attr("y", -6)
.style("text-anchor", "end")
.text("Day");
svg.append("g")
.attr("class", "y axis")
.call(yAxis)
.append("text")
.attr("class", "label")
.attr("transform", "rotate(-90)")
.attr("y", 6)
.attr("dy", ".71em")
.style("text-anchor", "end")
.text("Hour")
var circles = svg.selectAll(".dot")
.data(data)
.enter().append("circle")
.attr("class", "dot")
.attr("r", 3.5)
.attr("cx", function(d) { return x(d.Hour); })
.attr("cy", function(d) { return y(d.day); })
.style("fill", function(d) { return color(d.name); });
var legend = svg.selectAll(".legend")
.data(color.domain())
.enter().append("g")
.attr("class", "legend")
.attr("transform", function(d, i) { return "translate(0," + i * 20 + ")"; });
legend.append("rect")
.attr("x", width - 18)
.attr("width", 18)
.attr("height", 18)
.style("fill", color);
legend.append("text")
.attr("x", width - 24)
.attr("y", 9)
.attr("dy", ".35em")
.style("text-anchor", "end")
.text(function(d) { return d; });
d3.selectAll("[name=v]").on("change", function() {
var selected = this.value;
display = this.checked ? "inline" : "none";
svg.selectAll(".dot")
.filter(function(d) {return selected == d.name;})
.attr("display", display);
});
d3.selectAll("[name=sepal]").on("change", function(d) {
radius = this.value;
svg.selectAll(".dot")
console.log(radius);
circles.attr("r", function(d) { return d[radius]; });
});
d3.select("[name=xAX]").on("change", function(){
xAxy = this.value;
console.log(xAxy)
x.domain(d3.extent(data, function(d) { return d[xAxy]; })).nice();
svg.select(".x.axis").transition().call(xAxis);
svg.selectAll(".dot").transition().attr("cx", function(d) {
return x(d[xAxy]);
});
svg.selectAll(".x.axis").selectAll("text.label").text(axisNames[xAxy] + " (cm)");
});
d3.select("[name=yAX]").on("change", function(){
yAxy = this.value;
console.log(yAxy)
y.domain(d3.extent(data, function(d) { return d[yAxy]; })).nice();
svg.select(".y.axis").transition().call(yAxis);
svg.selectAll(".dot").transition().attr("cy", function(d) {
return y(d[yAxy]);
});
svg.selectAll(".y.axis").selectAll("text.label").text(axisNames[yAxy] + " (cm)");
});
});
</script>
<br><br>
<br>
</body>
结果
aa<- data.frame(a=c(1),b=c(2),length=c(3),date=as.Date("28.12.2016",format="%d.%m.%Y"))
aa <- aa[rep(row.names(aa), aa$length), 1:4]
aa <- as.data.table(aa)
aa[,row:=.I]
aa[,date:=date+row-1]
aa[,row:=NULL]