Mace - 8 months ago 60

R Question

I'm simply trying to find out if there in

`Matlab`

`head()`

`R`

`table`

`var1 = transpose(1:6);`

var2 = transpose(2:7);

aa = table(var1,var2);

I am looking for a funtion

`xx`

`aa(1:5,:)`

ans =

`var1 var2`

____ ____

1 2

2 3

3 4

4 5

5 6

something like:

`xx(aa)`

I could of course keep using the indexes above, but it would be more convenient with a function. I have used

`head()`

`R`

Answer

If all you want is the top few rows, then just type `mydata[1:5,]`

.

I use the following toy, from a certain someone's :-) `cgwtools`

package, which allows you to specify how many items to display and has an option to skip a few elements if desired. As it happens, I chose not to retain dimensions, to make it easier to examine a `list`

object. Feel free to take this code & modify to your needs (e.g. remove the part which returns the last `numel`

elements 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 (Stackoverflow)