Function similar to head () in Matlab
I'm just trying to figure out if there is a Matlab
simple equivalent head()
in R
? It should display / print the top 5 lines of the array. So the following is giventable
var1 = transpose(1:6);
var2 = transpose(2:7);
aa = table(var1,var2);
I'm looking for a funtion xx
that produces the same as:
aa(1:5,:)
ans =
var1 var2
____ ____
1 2
2 3
3 4
4 5
5 6
something like:
xx(aa)
I could of course continue to use the indices above, but it would be more convenient with a function. I have used head()
extensively in R
.
source to share
If all you want is the top few lines, just type mydata[1:5,]
.
I am using the following toy from some someone :-) cgwtools
package that allows you to specify how many items to display and has the ability to skip multiple items if needed. Anyway, I decided not to keep the dimensions to make it easier to explore the object list
. Feel free to use this code and modify it (for example, remove the part that returns the last elements numel
if you only want the head of your data).
short <-function (x = seq(1, 20), numel = 4, skipel = 0, ynam = deparse(substitute(x)))
{
ynam <- as.character(ynam)
ynam <- gsub(" ", "", ynam)
if (is.list(x))
x <- unlist(t(x))
if (2 * numel >= length(x)) {
print(x)
}
else {
frist = 1 + skipel
last = numel + skipel
cat(paste(ynam, "[", frist, "] thru ", ynam, "[", last,
"]\n", sep = ""))
print(x[frist:last])
cat(" ... \n")
cat(paste(ynam, "[", length(x) - numel - skipel + 1,
"] thru ", ynam, "[", length(x) - skipel, "]\n",
sep = ""))
print(x[(length(x) - numel - skipel + 1):(length(x) -
skipel)])
}
}
source to share
Here are two functions equivalent to head () and tail () in R. You can simply copy / paste the source code into the .m file and save it in the directory you want:
function head(data, varargin)
%head - displays the first n rows of an array
%
% Syntax: head(data, n)
%
% Inputs:
% data - data to display (table, double)
% n - number of rows (integer, optional input)
%
% Author: Daniel A. Brodén
% KTH, Royal Institute of Technology, Osquldas Väg 10, 100 44, Stockholm
% email: danbro@kth.se
% Website: http://www.kth.se/profile/danbro
% August 2015; Last revision: Aug-2015
% Define parser object
p = inputParser;
% Required inputs
addRequired(p, 'data')
% Default values
default_n = 10;
% Optional inputs
checkInt = @(x) validateattributes(x,{'numeric'},{'integer','positive'});
addOptional(p, 'n', default_n, checkInt)
% Parse inputs
parse(p, data, varargin{:})
% Conditions
if size(p.Results.data, 1) < p.Results.n
error('Not enough rows')
end
% Return
data(1:p.Results.n,:)
end
Code for the tail
function tail(data, varargin)
%tail - displays the last n rows of an array
%
% Syntax: tail(data, n)
%
% Inputs:
% data - data to display (table, double)
% n - number of rows (integer, optional input)
%
% Author: Daniel A. Brodén
% KTH, Royal Institute of Technology, Osquldas Väg 10, 100 44, Stockholm
% email: danbro@kth.se
% Website: http://www.kth.se/profile/danbro
% August 2015; Last revision: Aug-2015
% Parser object
p = inputParser;
% Required inputs
addRequired(p, 'data')
% Default values
default_n = 10;
% Optional inputs
checkInt = @(x) validateattributes(x,{'numeric'},{'integer','positive'});
addOptional(p, 'n', default_n, checkInt)
% Parse inputs
parse(p, data, varargin{:})
% Conditions
if size(p.Results.data, 1) < p.Results.n
error('Not enough rows')
end
% Return
data((size(data,1)-p.Results.n + 1):size(data,1),:)
end
source to share