Repeat Vector When Its Length Is Not a Multiple of Desired Total Length

Repeat vector when its length is not a multiple of desired total length

Something, like this?

df <- data.frame(rnorm(1666))
df$cutter <- rep(1:5, length.out=1666)

tail(df)
rnorm.1666. cutter
1661 0.11693169 1
1662 -1.12508091 2
1663 0.25441847 3
1664 -0.06045037 4
1665 -0.17242921 5
1666 -0.85366242 1

Error in R: I should use rep() but I already do

You can use rep_len:

dat$colour <- rep_len(rainbow(4), nrow(dat))

R repeat n times

there is this rep_len that might be what you are looking for

rep_len(1:7, length.out = length(objects))
# [1] 1 2 3 4 5 6 7 1 2 3

What is causing my for/if loop to return 'longer object length is not a multiple of shorter object length' warning in R

"The vectors are taken from loading in an excel file and using 'x <- S1L1$PercentageCoverage' and 'y <- S1L1$FoldCount'."

It looks like you have S1L1 as a dataframe with PercentageCoverage and FoldCount columns and want to create a new column PA when PercentageCoverage >= 1 and FoldCount>=70, then PA should be PercentageCoverage else 0, correct?

S1L1$PA <- ifelse(S1L1$PercentageCoverage>=1 & S1L1$FoldCount>=70, S1L1$PercentageCoverage, 0)

Create dummy data with LETTERS[1:3] recycled as a new feature on a df of varying length

As provided in the documentation for mutate(), the value can be:

A vector of length 1, which will be recycled to the correct length.

A vector the same length as the current group (or the whole data frame
if ungrouped).

NULL, to remove the column.

A data frame or tibble, to create multiple columns in the output.

Therefore, it won't recycle the vector to the length of the df. You can, however, do:

diamonds %>% 
mutate(DummyCategory = rep(LETTERS[1:3], length.out = n()))

carat cut color clarity depth table price x y z DummyCategory
<dbl> <ord> <ord> <ord> <dbl> <dbl> <int> <dbl> <dbl> <dbl> <chr>
1 0.23 Ideal E SI2 61.5 55 326 3.95 3.98 2.43 A
2 0.21 Premium E SI1 59.8 61 326 3.89 3.84 2.31 B
3 0.23 Good E VS1 56.9 65 327 4.05 4.07 2.31 C
4 0.290 Premium I VS2 62.4 58 334 4.2 4.23 2.63 A
5 0.31 Good J SI2 63.3 58 335 4.34 4.35 2.75 B
6 0.24 Very Good J VVS2 62.8 57 336 3.94 3.96 2.48 C
7 0.24 Very Good I VVS1 62.3 57 336 3.95 3.98 2.47 A
8 0.26 Very Good H SI1 61.9 55 337 4.07 4.11 2.53 B
9 0.22 Fair E VS2 65.1 61 337 3.87 3.78 2.49 C
10 0.23 Very Good H VS1 59.4 61 338 4 4.05 2.39 A

R doesn't accept inserting vector when its length is big

R has a limit on the string length allowed in the console. This is discussed in detail here which suggests a 4,095 byte limit to avoid an overflow issue.

Regarding a good resource for R, you can browse the official documentation on a function by typing ? followed by the function name in the console, e.g. ?c would bring up the documentation on the c function. For a good introduction there are a lot of good books on R such as those by Hadley Wickham.

Repeat vector to fill down column in data frame

If the vector can be evenly recycled, into the data.frame, you do not get and error or a warning:

df <- data.frame(x = 1:10)
df$z <- 1:5

This may be what you were experiencing before.

You can get your vector to fit as you mention with rep_len:

df$y <- rep_len(1:3, length.out=10)

This results in

df
x z y
1 1 1 1
2 2 2 2
3 3 3 3
4 4 4 1
5 5 5 2
6 6 1 3
7 7 2 1
8 8 3 2
9 9 4 3
10 10 5 1

Note that in place of rep_len, you could use the more common rep function:

df$y <- rep(1:3,len=10)

From the help file for rep:

rep.int and rep_len are faster simplified versions for two common cases. They are not generic.

R - Sequentially order data frame, repeat value if variable remains the same and change it when it changes

You could use data.table:

library(data.table)
setDT(df)
df[, seq := rleid(school), by = id]

df
id school seq
1: 1 school_1 1
2: 1 school_2 2
3: 1 school_3 3
4: 1 school_3 3
5: 1 school_4 4
6: 2 school_5 1
7: 2 school_1 2
8: 2 school_1 2
9: 2 school_1 2
10: 2 school_4 3
11: 3 school_6 1
.....


Related Topics



Leave a reply



Submit