R/randomizeidentities.R

Defines functions randomizeidentities

Documented in randomizeidentities

randomizeidentities <-
function(raw, withinvertexfrom, byvertexfrom, withreplacement=TRUE)
{
	if (byvertexfrom)
	{
		if (withinvertexfrom)
		{
			list<-by(raw,raw$VertexFrom, function(x) { neworder <- sample(nrow(x), replace= withreplacement); x$VertexTo <- x$VertexTo[neworder]; return(x)})
			outdf <- do.call(rbind.fill, list)	
		}
		else
		{
			neworder <- sample(nrow(raw),replace=withreplacement)
			outdf <- raw
			outdf$VertexTo <- outdf$VertexTo[neworder]
		}
	}
	else
	{
		if (withinvertexfrom)
		{
			list<-by(raw,raw$VertexTo, function(x) { neworder <- sample(nrow(x), replace= withreplacement); x$VertexFrom <- x$VertexFrom[neworder]; return(x)})
			outdf <- do.call(rbind.fill, list)	
		}
		else
		{
			neworder <- sample(nrow(raw), replace= withreplacement)
			outdf <- raw
			outdf$VertexFrom <- outdf$VertexFrom[neworder]
		}		
	}
	
	# remove loops
	outdf$VertexFrom <- as.character(outdf$VertexFrom)
	outdf$VertexTo <- as.character(outdf$VertexTo)

	return(subset(outdf,outdf$VertexFrom!=outdf$VertexTo))
}

Try the timeordered package in your browser

Any scripts or data that you put into this service are public.

timeordered documentation built on Aug. 21, 2023, 1:06 a.m.