Prefix selected column names in R - r

I have a df as follows:
Code LA.Name 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
1 E06000001 Hartlepool 25 20 27 22 15 20 18 17 16 15 16 23 16 28 29 34 40 36 39
2 E06000002 Middlesbrough 46 30 30 36 18 19 19 30 31 31 24 17 37 66 64 76 60 61 48
3 E06000003 Redcar and Cleveland 39 40 24 23 26 42 33 36 31 28 20 27 32 29 45 49 49 39 33
4 E06000004 Stockton-on-Tees 38 46 33 49 26 40 23 23 35 39 35 40 33 49 67 52 36 47 49
5 E06000005 Darlington 23 31 27 21 23 17 19 29 33 19 21 22 22 28 33 39 36 33 34
6 E06000006 Halton 36 21 34 31 27 24 29 20 16 30 20 24 27 33 40 52 37 37 33
>
As you can see, columns 3:21 are named with numerics, I want to prefix the column names with "w". How can I do this without typing out a list of 22 new column names?

If by chance you are within a dplyr pipeline, you can also use
library(dplyr)
library(stringr)
df %>%
rename_at(1:19, ~str_c("w", .))

names(df)[3:21] <- paste0("w", names(df)[3:21])

You can use some regular expression to identify the numbers in the column names and modify them accordingly
names(df) <- gsub("([0-9]{1,})", "w\\1", names(df)

Solution with dplyr but not needing stringr
library(dplyr)
df %>%
rename_at(3:21, ~paste0("w", .))

Related

ifelse() not working correctly using mutate: R

I have a small doubt in ifelse confition. I've read numerous articles and tried different solutions but I'm not able to solve it. Please help me with it.
I have a column of numbers ranging from 0:59 and it recurs like this for 500 rows.
I am trying to create another column which divides 0:29 as 0 and 30:59 as 30.
Data:
> data$Minute
[1] 0 0 0 1 1 1 2 2 2 2 2 2 3 3 4 4 4 5 5 5 5 5 6 6 6 7 7 7 7 7 8 8 8 8 8 8 9
[38] 10 10 10 10 10 10 10 10 10 11 11 11 11 11 11 11 11 11 11 12 12 12 12 13 13 13 13 13 14 14 14 14 14 14 15 15 15
[75] 15 15 15 15 15 15 16 16 16 16 16 17 17 18 18 18 18 18 19 19 19 19 20 20 20 20 20 20 20 20 21 21 21 21 21 21 21
[112] 22 22 22 22 22 23 23 23 23 23 23 23 23 23 23 23 23 23 23 24 24 24 24 24 24 24 24 24 25 25 25 25 25 25 25 25 25
[149] 26 26 26 26 26 26 26 26 26 26 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 29
[186] 29 29 29 29 29 29 29 29 29 29 29 30 30 30 30 30 30 30 30 30 30 30 30 30 31 31 31 31 31 31 31 31 31 31 31 31 31
[223] 31 32 32 32 32 32 33 33 33 33 33 33 33 33 33 33 33 34 34 34 34 34 34 34 34 34 34 34 34 34 35 35 35 35 35 35 35
[260] 35 35 35 36 36 36 36 36 36 36 36 37 37 37 37 37 37 37 37 37 38 38 38 38 38 38 38 38 38 38 39 39 39 39 39 39 39
[297] 39 39 40 40 40 40 40 40 40 40 40 40 40 40 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 42 42 42 42 42 42 42
[334] 42 42 42 42 43 43 43 43 43 43 43 43 43 43 43 43 43 43 43 43 44 44 44 44 44 44 44 44 44 45 45 45 45 45 45 45 45
[371] 45 45 45 45 45 45 45 45 46 46 46 46 46 46 46 47 47 47 47 47 47 47 47 47 47 47 47 47 48 48 48 48 48 48 48 48 48
[408] 48 48 48 49 49 49 49 49 49 49 50 50 50 50 50 50 50 50 50 50 50 50 50 50 51 51 51 51 51 51 51 51 51 51 52 52 52
[445] 52 52 52 52 52 52 52 52 52 53 53 53 53 53 53 53 53 54 54 54 54 54 54 54 54 54 54 54 55 55 55 55 55 55 55 55 55
[482] 55 56 56 56 56 56 56 56 56 56 56 56 56 56 57 57 57 57 57 57 57 57 58 58 58 58 58 58 58 58 58 59 59 59 59 59 59
[519] 59 59 59 59 59 59 59 0 0 0 0
Code:
data<- data %>% mutate(Period = ifelse(((Minute >=0) && (Minute <= 30)),0, 30) )
While running this code, I am only getting Period value as 0 for all the data points. Can you please help me with this small issue?
Thanks in Advance!
Try this
data %>% mutate(Period = ifelse(minute <= 30, 0, 30))

R plot numbers of factor levels having n, n+1, .... counts

I have a very large dataset (> 200000 lines) with 6 variables (only the first two shown)
>head(gt7)
ChromKey POS
1 2447 25
2 2447 183
3 26341 75
4 26341 2213
5 26341 2617
6 54011 1868
I have converted the Chromkey variable to a factor variable made up of > 55000 levels.
> gt7[1] <- lapply(gt7[1], factor)
> is.factor(gt7$ChromKey)
[1] TRUE
I can further make a table with counts of ChromKey levels
> table(gt7$ChromKey)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
88 88 44 33 11 11 33 22 121 11 22 11 11 11 22 11 33
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
22 22 44 55 22 11 22 66 11 11 11 22 11 11 11 187 77
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
77 11 44 11 11 11 11 11 11 22 66 11 22 11 44 22 22
... outut cropped
Which I can save in table format
> table <- table(gt7$ChromKey)
> head(table)
1 2 3 4 5 6
88 88 44 33 11 11
I would like to know whether is it possible to have a table (and histogram) of the number of levels with specific count numbers. From the example above, I would expect
88 44 33 11
2 1 1 2
I would very much appreciate any hint.
We can apply table again on the output to get the frequency count of the frequency
table(table(gt7$ChromKey))

How to assign values to the i-th index of a list using a loop?

I have integer values which are called player.i.team.j.coach.k where i ranges over the values 1-11, j ranges over the values 1-30 and k ranges over the values 1-10.
I'm trying to store the 11 players of each team j and coach k in a list (each coach will be assigned to 30 teams), for example
team.j.coach.k <- c(player.1.team.j.coach.k, player.2.team.j.coach.k,
player.3.team.j.coach.k, player.4.team.j.coach.k,
player.5.team.j.coach.k, player.6.team.j.coach.k,
player.7.team.j.coach.k, player.8.team.j.coach.k,
player.9.team.j.coach.k, player.10.team.j.coach.k,
player.11.team.j.coach.k)
And I'm trying to use loops for this. The problem is that my code is not working:
First I define empty lists where I will store my teams:
for (j in 1:30) {
for (k in 1:10) {
assign(paste0("team.",j,".coach.",k),c())
}
}
So for example
> team.1.coach.1
NULL
But now the following code throws an error
for (i in 1:11) {
for (j in 1:30) {
for (k in 1:10) {
assign(get(paste0("team.", j, ".coach.", k))[i],
get(paste0("player.",i,".team.",j,".coach.",k)))
}
}
}
and the error is "invalid first argument". Alternatively, the following code throws an error as well
for (i in 1:11) {
for (j in 1:30) {
for (k in 1:10) {
get(paste0("team.", j, ".coach.", k))[i] <- get(paste0("player.", i, ".team.", j, ".coach.", k))
}
}
}
Where the error is "target of assignment expands to non-language object".
Note: I created the objects player.i.team.j.coach.k using assign() and a loop, that's why they are stored in a list yet.
x = c(3,5,6)
x[2] = 9
x # [1] 3 9 6
Did this help? (Re- assign a value to the i-th index of the list without assign and using a loop)
OK, after struggling so much and watching my reputation going down the toilet due to several downvotes, I managed to solve my problem. I used a variable count inside my loop to store everything as a single list, and then splitting my list into my desired lists:
my.list<-c()
count=0
for (c in 1:10){for (b in 1:30){for (a in 1:11){assign(paste0("player.",a,".team.",b,".coach.",c),a+b+c);count=count+1;my.list[count]<-get(paste0("player.",a,".team.",b,".coach.",c))}}}
And now I split my.list to get the lists that I want:
for (c in 1:10){for (b in 1:30){assign(paste0("team.",b,".coach.",c),my.list[(11*(b+30*(c-1))-10):(11*(b+30*(c-1)))])}}
And I get exactly what I want:
> for (b in 1:30){for (c in 1:10){print(get(paste0("team.",b,".coach.",c)))}}
[1] 3 4 5 6 7 8 9 10 11 12 13
[1] 4 5 6 7 8 9 10 11 12 13 14
[1] 5 6 7 8 9 10 11 12 13 14 15
[1] 6 7 8 9 10 11 12 13 14 15 16
[1] 7 8 9 10 11 12 13 14 15 16 17
[1] 8 9 10 11 12 13 14 15 16 17 18
[1] 9 10 11 12 13 14 15 16 17 18 19
[1] 10 11 12 13 14 15 16 17 18 19 20
[1] 11 12 13 14 15 16 17 18 19 20 21
[1] 12 13 14 15 16 17 18 19 20 21 22
[1] 4 5 6 7 8 9 10 11 12 13 14
[1] 5 6 7 8 9 10 11 12 13 14 15
[1] 6 7 8 9 10 11 12 13 14 15 16
[1] 7 8 9 10 11 12 13 14 15 16 17
[1] 8 9 10 11 12 13 14 15 16 17 18
[1] 9 10 11 12 13 14 15 16 17 18 19
[1] 10 11 12 13 14 15 16 17 18 19 20
[1] 11 12 13 14 15 16 17 18 19 20 21
[1] 12 13 14 15 16 17 18 19 20 21 22
[1] 13 14 15 16 17 18 19 20 21 22 23
[1] 5 6 7 8 9 10 11 12 13 14 15
[1] 6 7 8 9 10 11 12 13 14 15 16
[1] 7 8 9 10 11 12 13 14 15 16 17
[1] 8 9 10 11 12 13 14 15 16 17 18
[1] 9 10 11 12 13 14 15 16 17 18 19
[1] 10 11 12 13 14 15 16 17 18 19 20
[1] 11 12 13 14 15 16 17 18 19 20 21
[1] 12 13 14 15 16 17 18 19 20 21 22
[1] 13 14 15 16 17 18 19 20 21 22 23
[1] 14 15 16 17 18 19 20 21 22 23 24
[1] 6 7 8 9 10 11 12 13 14 15 16
[1] 7 8 9 10 11 12 13 14 15 16 17
[1] 8 9 10 11 12 13 14 15 16 17 18
[1] 9 10 11 12 13 14 15 16 17 18 19
[1] 10 11 12 13 14 15 16 17 18 19 20
[1] 11 12 13 14 15 16 17 18 19 20 21
[1] 12 13 14 15 16 17 18 19 20 21 22
[1] 13 14 15 16 17 18 19 20 21 22 23
[1] 14 15 16 17 18 19 20 21 22 23 24
[1] 15 16 17 18 19 20 21 22 23 24 25
[1] 7 8 9 10 11 12 13 14 15 16 17
[1] 8 9 10 11 12 13 14 15 16 17 18
[1] 9 10 11 12 13 14 15 16 17 18 19
[1] 10 11 12 13 14 15 16 17 18 19 20
[1] 11 12 13 14 15 16 17 18 19 20 21
[1] 12 13 14 15 16 17 18 19 20 21 22
[1] 13 14 15 16 17 18 19 20 21 22 23
[1] 14 15 16 17 18 19 20 21 22 23 24
[1] 15 16 17 18 19 20 21 22 23 24 25
[1] 16 17 18 19 20 21 22 23 24 25 26
[1] 8 9 10 11 12 13 14 15 16 17 18
[1] 9 10 11 12 13 14 15 16 17 18 19
[1] 10 11 12 13 14 15 16 17 18 19 20
[1] 11 12 13 14 15 16 17 18 19 20 21
[1] 12 13 14 15 16 17 18 19 20 21 22
[1] 13 14 15 16 17 18 19 20 21 22 23
[1] 14 15 16 17 18 19 20 21 22 23 24
[1] 15 16 17 18 19 20 21 22 23 24 25
[1] 16 17 18 19 20 21 22 23 24 25 26
[1] 17 18 19 20 21 22 23 24 25 26 27
[1] 9 10 11 12 13 14 15 16 17 18 19
[1] 10 11 12 13 14 15 16 17 18 19 20
[1] 11 12 13 14 15 16 17 18 19 20 21
[1] 12 13 14 15 16 17 18 19 20 21 22
[1] 13 14 15 16 17 18 19 20 21 22 23
[1] 14 15 16 17 18 19 20 21 22 23 24
[1] 15 16 17 18 19 20 21 22 23 24 25
[1] 16 17 18 19 20 21 22 23 24 25 26
[1] 17 18 19 20 21 22 23 24 25 26 27
[1] 18 19 20 21 22 23 24 25 26 27 28
[1] 10 11 12 13 14 15 16 17 18 19 20
[1] 11 12 13 14 15 16 17 18 19 20 21
[1] 12 13 14 15 16 17 18 19 20 21 22
[1] 13 14 15 16 17 18 19 20 21 22 23
[1] 14 15 16 17 18 19 20 21 22 23 24
[1] 15 16 17 18 19 20 21 22 23 24 25
[1] 16 17 18 19 20 21 22 23 24 25 26
[1] 17 18 19 20 21 22 23 24 25 26 27
[1] 18 19 20 21 22 23 24 25 26 27 28
[1] 19 20 21 22 23 24 25 26 27 28 29
[1] 11 12 13 14 15 16 17 18 19 20 21
[1] 12 13 14 15 16 17 18 19 20 21 22
[1] 13 14 15 16 17 18 19 20 21 22 23
[1] 14 15 16 17 18 19 20 21 22 23 24
[1] 15 16 17 18 19 20 21 22 23 24 25
[1] 16 17 18 19 20 21 22 23 24 25 26
[1] 17 18 19 20 21 22 23 24 25 26 27
[1] 18 19 20 21 22 23 24 25 26 27 28
[1] 19 20 21 22 23 24 25 26 27 28 29
[1] 20 21 22 23 24 25 26 27 28 29 30
[1] 12 13 14 15 16 17 18 19 20 21 22
[1] 13 14 15 16 17 18 19 20 21 22 23
[1] 14 15 16 17 18 19 20 21 22 23 24
[1] 15 16 17 18 19 20 21 22 23 24 25
[1] 16 17 18 19 20 21 22 23 24 25 26
[1] 17 18 19 20 21 22 23 24 25 26 27
[1] 18 19 20 21 22 23 24 25 26 27 28
[1] 19 20 21 22 23 24 25 26 27 28 29
[1] 20 21 22 23 24 25 26 27 28 29 30
[1] 21 22 23 24 25 26 27 28 29 30 31
[1] 13 14 15 16 17 18 19 20 21 22 23
[1] 14 15 16 17 18 19 20 21 22 23 24
[1] 15 16 17 18 19 20 21 22 23 24 25
[1] 16 17 18 19 20 21 22 23 24 25 26
[1] 17 18 19 20 21 22 23 24 25 26 27
[1] 18 19 20 21 22 23 24 25 26 27 28
[1] 19 20 21 22 23 24 25 26 27 28 29
[1] 20 21 22 23 24 25 26 27 28 29 30
[1] 21 22 23 24 25 26 27 28 29 30 31
[1] 22 23 24 25 26 27 28 29 30 31 32
[1] 14 15 16 17 18 19 20 21 22 23 24
[1] 15 16 17 18 19 20 21 22 23 24 25
[1] 16 17 18 19 20 21 22 23 24 25 26
[1] 17 18 19 20 21 22 23 24 25 26 27
[1] 18 19 20 21 22 23 24 25 26 27 28
[1] 19 20 21 22 23 24 25 26 27 28 29
[1] 20 21 22 23 24 25 26 27 28 29 30
[1] 21 22 23 24 25 26 27 28 29 30 31
[1] 22 23 24 25 26 27 28 29 30 31 32
[1] 23 24 25 26 27 28 29 30 31 32 33
[1] 15 16 17 18 19 20 21 22 23 24 25
[1] 16 17 18 19 20 21 22 23 24 25 26
[1] 17 18 19 20 21 22 23 24 25 26 27
[1] 18 19 20 21 22 23 24 25 26 27 28
[1] 19 20 21 22 23 24 25 26 27 28 29
[1] 20 21 22 23 24 25 26 27 28 29 30
[1] 21 22 23 24 25 26 27 28 29 30 31
[1] 22 23 24 25 26 27 28 29 30 31 32
[1] 23 24 25 26 27 28 29 30 31 32 33
[1] 24 25 26 27 28 29 30 31 32 33 34
[1] 16 17 18 19 20 21 22 23 24 25 26
[1] 17 18 19 20 21 22 23 24 25 26 27
[1] 18 19 20 21 22 23 24 25 26 27 28
[1] 19 20 21 22 23 24 25 26 27 28 29
[1] 20 21 22 23 24 25 26 27 28 29 30
[1] 21 22 23 24 25 26 27 28 29 30 31
[1] 22 23 24 25 26 27 28 29 30 31 32
[1] 23 24 25 26 27 28 29 30 31 32 33
[1] 24 25 26 27 28 29 30 31 32 33 34
[1] 25 26 27 28 29 30 31 32 33 34 35
[1] 17 18 19 20 21 22 23 24 25 26 27
[1] 18 19 20 21 22 23 24 25 26 27 28
[1] 19 20 21 22 23 24 25 26 27 28 29
[1] 20 21 22 23 24 25 26 27 28 29 30
[1] 21 22 23 24 25 26 27 28 29 30 31
[1] 22 23 24 25 26 27 28 29 30 31 32
[1] 23 24 25 26 27 28 29 30 31 32 33
[1] 24 25 26 27 28 29 30 31 32 33 34
[1] 25 26 27 28 29 30 31 32 33 34 35
[1] 26 27 28 29 30 31 32 33 34 35 36
[1] 18 19 20 21 22 23 24 25 26 27 28
[1] 19 20 21 22 23 24 25 26 27 28 29
[1] 20 21 22 23 24 25 26 27 28 29 30
[1] 21 22 23 24 25 26 27 28 29 30 31
[1] 22 23 24 25 26 27 28 29 30 31 32
[1] 23 24 25 26 27 28 29 30 31 32 33
[1] 24 25 26 27 28 29 30 31 32 33 34
[1] 25 26 27 28 29 30 31 32 33 34 35
[1] 26 27 28 29 30 31 32 33 34 35 36
[1] 27 28 29 30 31 32 33 34 35 36 37
[1] 19 20 21 22 23 24 25 26 27 28 29
[1] 20 21 22 23 24 25 26 27 28 29 30
[1] 21 22 23 24 25 26 27 28 29 30 31
[1] 22 23 24 25 26 27 28 29 30 31 32
[1] 23 24 25 26 27 28 29 30 31 32 33
[1] 24 25 26 27 28 29 30 31 32 33 34
[1] 25 26 27 28 29 30 31 32 33 34 35
[1] 26 27 28 29 30 31 32 33 34 35 36
[1] 27 28 29 30 31 32 33 34 35 36 37
[1] 28 29 30 31 32 33 34 35 36 37 38
[1] 20 21 22 23 24 25 26 27 28 29 30
[1] 21 22 23 24 25 26 27 28 29 30 31
[1] 22 23 24 25 26 27 28 29 30 31 32
[1] 23 24 25 26 27 28 29 30 31 32 33
[1] 24 25 26 27 28 29 30 31 32 33 34
[1] 25 26 27 28 29 30 31 32 33 34 35
[1] 26 27 28 29 30 31 32 33 34 35 36
[1] 27 28 29 30 31 32 33 34 35 36 37
[1] 28 29 30 31 32 33 34 35 36 37 38
[1] 29 30 31 32 33 34 35 36 37 38 39
[1] 21 22 23 24 25 26 27 28 29 30 31
[1] 22 23 24 25 26 27 28 29 30 31 32
[1] 23 24 25 26 27 28 29 30 31 32 33
[1] 24 25 26 27 28 29 30 31 32 33 34
[1] 25 26 27 28 29 30 31 32 33 34 35
[1] 26 27 28 29 30 31 32 33 34 35 36
[1] 27 28 29 30 31 32 33 34 35 36 37
[1] 28 29 30 31 32 33 34 35 36 37 38
[1] 29 30 31 32 33 34 35 36 37 38 39
[1] 30 31 32 33 34 35 36 37 38 39 40
[1] 22 23 24 25 26 27 28 29 30 31 32
[1] 23 24 25 26 27 28 29 30 31 32 33
[1] 24 25 26 27 28 29 30 31 32 33 34
[1] 25 26 27 28 29 30 31 32 33 34 35
[1] 26 27 28 29 30 31 32 33 34 35 36
[1] 27 28 29 30 31 32 33 34 35 36 37
[1] 28 29 30 31 32 33 34 35 36 37 38
[1] 29 30 31 32 33 34 35 36 37 38 39
[1] 30 31 32 33 34 35 36 37 38 39 40
[1] 31 32 33 34 35 36 37 38 39 40 41
[1] 23 24 25 26 27 28 29 30 31 32 33
[1] 24 25 26 27 28 29 30 31 32 33 34
[1] 25 26 27 28 29 30 31 32 33 34 35
[1] 26 27 28 29 30 31 32 33 34 35 36
[1] 27 28 29 30 31 32 33 34 35 36 37
[1] 28 29 30 31 32 33 34 35 36 37 38
[1] 29 30 31 32 33 34 35 36 37 38 39
[1] 30 31 32 33 34 35 36 37 38 39 40
[1] 31 32 33 34 35 36 37 38 39 40 41
[1] 32 33 34 35 36 37 38 39 40 41 42
[1] 24 25 26 27 28 29 30 31 32 33 34
[1] 25 26 27 28 29 30 31 32 33 34 35
[1] 26 27 28 29 30 31 32 33 34 35 36
[1] 27 28 29 30 31 32 33 34 35 36 37
[1] 28 29 30 31 32 33 34 35 36 37 38
[1] 29 30 31 32 33 34 35 36 37 38 39
[1] 30 31 32 33 34 35 36 37 38 39 40
[1] 31 32 33 34 35 36 37 38 39 40 41
[1] 32 33 34 35 36 37 38 39 40 41 42
[1] 33 34 35 36 37 38 39 40 41 42 43
[1] 25 26 27 28 29 30 31 32 33 34 35
[1] 26 27 28 29 30 31 32 33 34 35 36
[1] 27 28 29 30 31 32 33 34 35 36 37
[1] 28 29 30 31 32 33 34 35 36 37 38
[1] 29 30 31 32 33 34 35 36 37 38 39
[1] 30 31 32 33 34 35 36 37 38 39 40
[1] 31 32 33 34 35 36 37 38 39 40 41
[1] 32 33 34 35 36 37 38 39 40 41 42
[1] 33 34 35 36 37 38 39 40 41 42 43
[1] 34 35 36 37 38 39 40 41 42 43 44
[1] 26 27 28 29 30 31 32 33 34 35 36
[1] 27 28 29 30 31 32 33 34 35 36 37
[1] 28 29 30 31 32 33 34 35 36 37 38
[1] 29 30 31 32 33 34 35 36 37 38 39
[1] 30 31 32 33 34 35 36 37 38 39 40
[1] 31 32 33 34 35 36 37 38 39 40 41
[1] 32 33 34 35 36 37 38 39 40 41 42
[1] 33 34 35 36 37 38 39 40 41 42 43
[1] 34 35 36 37 38 39 40 41 42 43 44
[1] 35 36 37 38 39 40 41 42 43 44 45
[1] 27 28 29 30 31 32 33 34 35 36 37
[1] 28 29 30 31 32 33 34 35 36 37 38
[1] 29 30 31 32 33 34 35 36 37 38 39
[1] 30 31 32 33 34 35 36 37 38 39 40
[1] 31 32 33 34 35 36 37 38 39 40 41
[1] 32 33 34 35 36 37 38 39 40 41 42
[1] 33 34 35 36 37 38 39 40 41 42 43
[1] 34 35 36 37 38 39 40 41 42 43 44
[1] 35 36 37 38 39 40 41 42 43 44 45
[1] 36 37 38 39 40 41 42 43 44 45 46
[1] 28 29 30 31 32 33 34 35 36 37 38
[1] 29 30 31 32 33 34 35 36 37 38 39
[1] 30 31 32 33 34 35 36 37 38 39 40
[1] 31 32 33 34 35 36 37 38 39 40 41
[1] 32 33 34 35 36 37 38 39 40 41 42
[1] 33 34 35 36 37 38 39 40 41 42 43
[1] 34 35 36 37 38 39 40 41 42 43 44
[1] 35 36 37 38 39 40 41 42 43 44 45
[1] 36 37 38 39 40 41 42 43 44 45 46
[1] 37 38 39 40 41 42 43 44 45 46 47
[1] 29 30 31 32 33 34 35 36 37 38 39
[1] 30 31 32 33 34 35 36 37 38 39 40
[1] 31 32 33 34 35 36 37 38 39 40 41
[1] 32 33 34 35 36 37 38 39 40 41 42
[1] 33 34 35 36 37 38 39 40 41 42 43
[1] 34 35 36 37 38 39 40 41 42 43 44
[1] 35 36 37 38 39 40 41 42 43 44 45
[1] 36 37 38 39 40 41 42 43 44 45 46
[1] 37 38 39 40 41 42 43 44 45 46 47
[1] 38 39 40 41 42 43 44 45 46 47 48
[1] 30 31 32 33 34 35 36 37 38 39 40
[1] 31 32 33 34 35 36 37 38 39 40 41
[1] 32 33 34 35 36 37 38 39 40 41 42
[1] 33 34 35 36 37 38 39 40 41 42 43
[1] 34 35 36 37 38 39 40 41 42 43 44
[1] 35 36 37 38 39 40 41 42 43 44 45
[1] 36 37 38 39 40 41 42 43 44 45 46
[1] 37 38 39 40 41 42 43 44 45 46 47
[1] 38 39 40 41 42 43 44 45 46 47 48
[1] 39 40 41 42 43 44 45 46 47 48 49
[1] 31 32 33 34 35 36 37 38 39 40 41
[1] 32 33 34 35 36 37 38 39 40 41 42
[1] 33 34 35 36 37 38 39 40 41 42 43
[1] 34 35 36 37 38 39 40 41 42 43 44
[1] 35 36 37 38 39 40 41 42 43 44 45
[1] 36 37 38 39 40 41 42 43 44 45 46
[1] 37 38 39 40 41 42 43 44 45 46 47
[1] 38 39 40 41 42 43 44 45 46 47 48
[1] 39 40 41 42 43 44 45 46 47 48 49
[1] 40 41 42 43 44 45 46 47 48 49 50
[1] 32 33 34 35 36 37 38 39 40 41 42
[1] 33 34 35 36 37 38 39 40 41 42 43
[1] 34 35 36 37 38 39 40 41 42 43 44
[1] 35 36 37 38 39 40 41 42 43 44 45
[1] 36 37 38 39 40 41 42 43 44 45 46
[1] 37 38 39 40 41 42 43 44 45 46 47
[1] 38 39 40 41 42 43 44 45 46 47 48
[1] 39 40 41 42 43 44 45 46 47 48 49
[1] 40 41 42 43 44 45 46 47 48 49 50
[1] 41 42 43 44 45 46 47 48 49 50 51

What is the name and reason for the [1] at the output prompt?

What's the name for the [1] below.
What is its significance?
Is it always only [1]? If not, then under what conditions is it something else? (example please)
> bb <- c(5,6,7)
> bb
[1] 5 6 7
It shows the count of the variables. In your case, it shows
bb <- c(5,6,7)
> bb
# [1] 5 6 7
Try,
c(1:50)
#[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] 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
You can also avoid that being displayed by using cat
cat(c(1:50))
#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

Creating a sequence in R [duplicate]

This question already has answers here:
Create integer sequences defined by 'from' and 'to' vectors
(2 answers)
Closed 5 years ago.
Let's say, I created two vectors like:
Ncla = 10
CC.1 = seq(2,((Ncla *Ncla)-Ncla),(Ncla+1))
CC.2 = seq(Ncla,((Ncla *Ncla)-Ncla),(Ncla))
and, I tried to create the following sequence:
#[1] 2 3 4 5 6 7 8 9 10 13 14 15 16 17 18 19 20 24 25 26
# 27 28 29 30 35 36 37 38 39 40 46 47 48 49 50 57 58 59 60 68 69 70 79 80 90
using the statement:
for(i in 1:(Ncla-1)) A.1[i]={c(seq(CC.1[i],CC.2[i],length = 1))}
but it doesn't work.
Any help is greatly appreciated.
Try
unlist(Map(seq, CC.1, CC.2))
# [1] 2 3 4 5 6 7 8 9 10 13 14 15 16 17 18 19 20 24 25 26 27 28 29 30 35
#[26] 36 37 38 39 40 46 47 48 49 50 57 58 59 60 68 69 70 79 80 90
Or
unlist(sapply(seq_along(CC.1), function(i) seq(CC.1[i], CC.2[i])))
Or
A.1 <- list()
for(i in seq_along(CC.1)) A.1[[i]] <- seq(CC.1[i], CC.2[i])
unlist(A.1)
# [1] 2 3 4 5 6 7 8 9 10 13 14 15 16 17 18 19 20 24 25 26 27 28 29 30 35
#[26] 36 37 38 39 40 46 47 48 49 50 57 58 59 60 68 69 70 79 80 90
test<-NULL
for(i in 1:(Ncla-1)) {
A.1=c(seq(CC.1[i],CC.2[i],1))
test<-c(test,A.1)
}
test
Your mistake: You were not saving your results.

Resources