Display vector in R with a defined viewport - r

I want to display a vector consistently in different R environment.
For example, for a vector like this
c(1:30)
will display 24 values per row
[1] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
[25] 25 26 27 28 29 30
and not
[1] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30

The closest thing to what you are looking for is to use options() to configure the width of the results window:
options(width = 75)
c(1:30)
[1] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
[24] 24 25 26 27 28 29 30

Related

Generating a vector with n repetitions of x, then y, then z, with a fixed upper bound

I am trying to create a vector where I have 3 repetitions of the number 1, then 3 repetitions of the number 2, and so on up to, for instance, 3 repetitions of the number 36.
c(1,1,1,2,2,2,3,3,3,4,4,4,5,5,5...)
I have tried the following use of rep() but got the following error:
Error in rep(3, seq(1:36)) : argument 'times' incorrect
What formulation do I need to use to properly generate the vector I want?
sort(rep(1:36, 3))
Or even better as #Wimpel mentioned in the comments, use the each argument of the rep function.
rep(1:36, each = 3)
output
# [1] 1 1 1 2 2 2 3 3 3 4 4 4 5 5 5 6 6 6 7 7 7 8 8 8 9 9 9 10 10 10 11 11 11 12 12 12 13 13 13 14 14 14 15 15 15 16 16 16 17 17 17 18 18 18 19 19 19 20 20 20 21 21 21 22
# [65] 22 22 23 23 23 24 24 24 25 25 25 26 26 26 27 27 27 28 28 28 29 29 29 30 30 30 31 31 31 32 32 32 33 33 33 34 34 34 35 35 35 36 36 36
This one should work. However probably not the most elegant.
reps = c()
n = 36
for(i in 1:n){
reps = append(reps, rep(i, 3))
}
reps
alternatively using the rep function properly (see documentation (?rep for argument each):
rep(1:36,each = 3)
rep approach is preferable (see existing answers)
Here are some other options:
> kronecker(1:36, rep(1, 3))
[1] 1 1 1 2 2 2 3 3 3 4 4 4 5 5 5 6 6 6 7 7 7 8 8 8 9
[26] 9 9 10 10 10 11 11 11 12 12 12 13 13 13 14 14 14 15 15 15 16 16 16 17 17
[51] 17 18 18 18 19 19 19 20 20 20 21 21 21 22 22 22 23 23 23 24 24 24 25 25 25
[76] 26 26 26 27 27 27 28 28 28 29 29 29 30 30 30 31 31 31 32 32 32 33 33 33 34
[101] 34 34 35 35 35 36 36 36
> c(outer(rep(1, 3), 1:36))
[1] 1 1 1 2 2 2 3 3 3 4 4 4 5 5 5 6 6 6 7 7 7 8 8 8 9
[26] 9 9 10 10 10 11 11 11 12 12 12 13 13 13 14 14 14 15 15 15 16 16 16 17 17
[51] 17 18 18 18 19 19 19 20 20 20 21 21 21 22 22 22 23 23 23 24 24 24 25 25 25
[76] 26 26 26 27 27 27 28 28 28 29 29 29 30 30 30 31 31 31 32 32 32 33 33 33 34
[101] 34 34 35 35 35 36 36 36

Order Levels (format: numbers) in a factor from low to high in RStudio

I have a problem in R. I have created a factor (called reference). But the Levels are not in the right order; I want them to be in order from low to high (1,2,3...30). I tried reference<-relevel(reference,1) but then the order is not right. Is there a way how I can change the order as I want?
reference
[1] 5 5 1 5 5 5 1 1 1 1 1 11 1 1 1 5 1 5 1 2 1 1 1 1 2 1 1 1 3 1 2 1 2 15 2 2 2 15
[39] 16 3 2 2 4 2 16 23 2 14 2 4 2 3 2 14 4 24 2 2 2 2 3 4 3 3 3 3 25 3 2 3 3 3 3 3 25 3
[77] 3 3 3 1 3 15 3 3 3 3 3 1 1 3 8 4 4 4 4 8 4 4 4 4 4 4 4 4 4 4 8 4 4 4 4 8 4 4
[115] 4 4 15 8 4 16 8 16 14 14 5 5 5 5 7 5 16 5 14 16 14 14 5 5 5 5 14 5 3 5 7 8 4 7 5 5 6 4
[153] 4 15 15 15 6 4 6 14 4 14 15 6 4 11 6 28 16 6 16 15 9 14 6 14 15 6 16 14 7 14 16 16 16 16 7 7 14 16
[191] 16 7 15 7 4 15 7 15 14 15 15 9 14 7 16 15 15 15 16 14 8 8 9 4 8 8 10 8 4 7 8 4 8 4 8 8 8 8
[229] 8 8 9 8 8 4 8 8 14 8 8 8 29 14 29 9 29 14 9 16 29 10 29 14 16 9 9 29 29 29 9 29 16 4 4 9 15 29
[267] 9 23 29 9 10 4 10 10 10 10 10 10 10 10 10 10 14 5 10 10 15 11 10 11 10 10 11 11 11 15 4 10 15 10 10 11 11 10
[305] 10 10 11 11 10 11 11 10 11 11 11 10 11 10 8 11 10 10 11 11 10 10 11 11 10 11 14 16 7 15 12 14 14 15 15 14 14 14
[343] 12 17 11 2 15 16 7 16 15 15 15 14 17 28 5 7 17 16 11 13 13 11 13 13 13 13 16 13 13 13 11 13 15 13 13 13 11 13
[381] 13 13 10 13 13 13 13 13 13 10 11 14 15 14 4 14 14 15 8 14 4 4 14 14 14 15 15 14 4 4 14 4 4 4 14 7 8 14
[419] 11 11 15 15 16 3 5 11 15 14 15 15 4 3 15 4 15 15 15 14 14 15 16 15 14 15 11 15 15 15 15 16 7 4 16 16 16 14
[457] 15 14 16 16 16 16 15 12 4 4 4 14 16 16 15 15 14 26 16 26 14 16 4 13 17 21 17 21 17 17 17 17 17 20 17 21 17 17
[495] 18 17 18 17 18 21 17 21 20 18 21 18 18 20 17 17 17 20 17 17 18 18 18 20 18 18 21 18 21 17 17 17 18 18 21 18 17 18
[533] 17 21 17 20 18 22 18 20 19 18 18 19 4 19 18 19 19 15 1 19 17 7 3 20 17 19 19 19 20 18 19 19 19 19 20 18 15 14
[571] 21 20 20 20 20 22 20 20 19 20 20 20 20 20 22 20 21 18 20 20 21 21 17 18 21 20 20 18 20 20 21 17 21 21 22 21 20 20
[609] 21 21 21 21 17 21 18 21 17 18 17 20 20 18 20 20 18 21 21 21 20 17 21 22 22 22 22 22 22 22 21 22 22 22 21 22 22 21
[647] 22 22 21 29 21 22 22 22 22 22 22 20 18 22 8 15 4 4 15 4 4 15 15 15 4 4 4 15 4 15 15 23 4 23 4 2 8 23
[685] 4 23 10 2 4 7 4 7 18 24 15 15 26 11 15 15 4 24 7 15 15 5 24 15 4 1 4 8 24 23 23 6 4 3 23 4 5 1
[723] 1 1 1 2 1 1 1 1 1 1 1 1 3 2 1 5 1 2 2 1 1 1 20 2 3 25 2 1 3 15 15 15 14 14 14 5 14 15
[761] 4 18 14 8 26 4 15 20 10 16 8 4 15 15 16 4 23 18 15 4 27 27 27 27 27 27 27 27 27 27 27 27 27 27 27 27 27 27
[799] 27 27 27 27 27 27 27 27 27 27 27 27 28 28 28 28 28 28 28 28 28 28 28 28 28 28 28 28 28 28 28 28 28 28 28 28 28 28
[837] 28 28 28 28 29 29 29 29 29 29 29 29 29 29 29 29 29 29 29 29 29 29 29 29 29 29 29 29 29 29 29 29 29 29 30 30 30 30
[875] 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30
Levels: 26 28 27 29 3 30 4 5 6 7 8 9 1 10 11 12 13 14 15 16 17 18 19 2 20 21 22 23 24 25
You can convert the factors to numbers and then factors again :
reference <- factor(as.numeric(as.character(reference)))
Or if you already know the range of factors :
reference <- factor(reference, 1:30)

which.min() returning two numbers

I need the position of the smallest value in my vector (degrees in a graph, got from function degree()). I use the which.min().
However as the vector itself is "anotated", I get two values - the name of the node and the position in the vector (which I have no idea why they are not in the right order) - here node "23" has the smallest degree and it is in the 40th position in the vector. They appear on top of each other and I cannot figure out how to separate them.
I need to use just the name of the node for further applications. I couldn't find any question about this issue.
> degs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 24 25 26 27 28 29 30 31 32 34 35 36 38 39 40 41 33 23 37 42 43
14 25 31 17 25 11 26 21 23 25 24 17 13 20 12 15 7 15 28 18 9 17 8 7 7 7 14 19 12 17 19 10 19 20 19 10 7 11 12 6 8 12 13
> which.min(degs)
23
40
The top number is just the name of the value and you can ignore it, see?
> c("23" = 40)
23
40
If you want just the name of the node, you can use
names(which.min(degs))
Output will be "23".

split a list and increment for loop by 10

How to split a list in r?
I want to split a list in increment manner.
for ex.:
x <- 1:50
n <- 5
spt <- split(x,cut(x,quantile(x,(0:n)/n), include.lowest=TRUE, labels=FALSE))
we get
$`1`
[1] 1 2 3 4 5 6 7 8 9 10
$`2`
[1] 11 12 13 14 15 16 17 18 19 20
$`3`
[1] 21 22 23 24 25 26 27 28 29 30
$`4`
[1] 31 32 33 34 35 36 37 38 39 40
$`5`
[1] 41 42 43 44 45 46 47 48 49 50
I don't want this output. I want the output like below,
$`1`
[1] 1 2 3 4 5 6 7 8 9 10
$`2`
[1] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
$`3`
[1] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
$`4`
[1] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 2021 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
$`5`
[1] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
41 42 43 44 45 46 47 48 49 50
any idea?
And i also want to know that how to increment for loop by 10 in r?
Thanks.
We can use seq
lapply(seq(10,50, by=10), function(i) x[1:i])
Or as #RichardScriven mentioned in the comments, the seq(10,50, by=10) can be replaced by 1:5 * 10L

split data based on cumulative value of column in r

I have following type of data:
myd <- data.frame (group = c(rep(1, 15), rep(2, 15)),
distance = c(0, 4, 8,9,11, 14,18,19,23, 24, 29,30,35,40, 43,
0, 8,9,9,12, 13,14,15,16, 18, 23,24,28, 29, 30),
var1 = c(1:15, 11:25), var2 = 1:30, var3 = 1:30)
myd
group distance var1 var2 var3
1 1 0 1 1 1
2 1 4 2 2 2
3 1 8 3 3 3
4 1 9 4 4 4
5 1 11 5 5 5
6 1 14 6 6 6
7 1 18 7 7 7
8 1 19 8 8 8
9 1 23 9 9 9
10 1 24 10 10 10
11 1 29 11 11 11
12 1 30 12 12 12
13 1 35 13 13 13
14 1 40 14 14 14
15 1 43 15 15 15
16 2 0 11 16 16
17 2 8 12 17 17
18 2 9 13 18 18
19 2 9 14 19 19
20 2 12 15 20 20
21 2 13 16 21 21
22 2 14 17 22 22
23 2 15 18 23 23
24 2 16 19 24 24
25 2 18 20 25 25
26 2 23 21 26 26
27 2 24 22 27 27
28 2 28 23 28 28
29 2 29 24 29 29
30 2 30 25 30 30
I have multiple group levels (than 2 shown above). For each distance (say mile posts in highway) starts with 0 and is cumulative to end to the group. I want to split the data (make bins) in such way that each group is approximately of distance 10. The resulting split data will be like:
data group1subset1
group distance var1 var2 var3
1 1 0 1 1 1
2 1 4 2 2 2
3 1 8 3 3 3
4 1 9 4 4 4
data group1subset2
5 1 11 5 5 5
6 1 14 6 6 6
7 1 18 7 7 7
8 1 19 8 8 8
data group1subset3
9 1 23 9 9 9
10 1 24 10 10 10
11 1 29 11 11 11
12 1 30 12 12 12
data group1subset4
13 1 35 13 13 13
14 1 40 14 14 14
data group1subset5
15 1 43 15 15 15
=====
data group2subset1
16 2 0 11 16 16
17 2 8 12 17 17
18 2 9 13 18 18
19 2 9 14 19 19
data group2subset2
20 2 12 15 20 20
21 2 13 16 21 21
22 2 14 17 22 22
23 2 15 18 23 23
24 2 16 19 24 24
25 2 18 20 25 25
data group2subset3
26 2 23 21 26 26
27 2 24 22 27 27
28 2 28 23 28 28
29 2 29 24 29 29
30 2 30 25 30 30
I need to automize the process as the real data is really big. Please suggest how can we do it?
I'd actually use cut to accomplish this:
maxd <- (max(myd$distance) %/% 10 * 10) + 10
transform(myd,cutdist = cut(distance, breaks = seq(0,maxd, by = 10),
include.lowest = TRUE))
group distance var1 var2 var3 cumdist cutdist
1 1 0 1 1 1 0 [0,10]
2 1 4 2 2 2 4 [0,10]
3 1 8 3 3 3 12 [0,10]
4 1 9 4 4 4 21 [0,10]
5 1 11 5 5 5 32 (10,20]
6 1 14 6 6 6 46 (10,20]
7 1 18 7 7 7 64 (10,20]
8 1 19 8 8 8 83 (10,20]
9 1 23 9 9 9 106 (20,30]
10 1 24 10 10 10 130 (20,30]
11 1 29 11 11 11 159 (20,30]
12 1 30 12 12 12 189 (20,30]
13 1 35 13 13 13 224 (30,40]
14 1 40 14 14 14 264 (30,40]
15 1 43 15 15 15 307 (40,50]
16 2 0 11 16 16 307 [0,10]
17 2 8 12 17 17 315 [0,10]
18 2 9 13 18 18 324 [0,10]
19 2 9 14 19 19 333 [0,10]
20 2 12 15 20 20 345 (10,20]
21 2 13 16 21 21 358 (10,20]
22 2 14 17 22 22 372 (10,20]
23 2 15 18 23 23 387 (10,20]
24 2 16 19 24 24 403 (10,20]
25 2 18 20 25 25 421 (10,20]
26 2 23 21 26 26 444 (20,30]
27 2 24 22 27 27 468 (20,30]
28 2 28 23 28 28 496 (20,30]
29 2 29 24 29 29 525 (20,30]
30 2 30 25 30 30 555 (20,30]
There's actually no need to calculate cumulative distance, since you want to keep them in groups of multiples of 10
What about
split(myd, interaction(myd$distance %/%10, myd$group, drop=F), drop=T)
here we bin the distance based on how many times it's divisible by 10 and create an interaction with group to make distinct categories for each. We us that as a factor for split(). This puts 0 with the first group and 10 with the second so some of your example breaks are slightly different with the code, but it seems comparable.

Resources