Max Length for a Vector in R

Maximum length of a vector in R is only 349?

Command lines entered at the console are limited to about 4095 bytes (not characters).

Source: R Documentation

You can try it yourself, if you insert a line break, it will work:

vector2 <- c("value1", "value2", "value3", "value4", "value5", "value6", "value7", "value8", "value9", "value10", "value11", "value12", "value13", "value14", "value15", "value16", "value17", "value18", "value19", "value20", "value21", "value22", "value23", "value24", "value25", "value26", "value27", "value28", "value29", "value30", "value31", "value32", "value33", "value34", "value35", "value36", "value37", "value38", "value39", "value40", "value41", "value42", "value43", "value44", "value45", "value46", "value47", "value48", "value49", "value50", "value51", "value52", "value53", "value54", "value55", "value56", "value57", "value58", "value59", "value60", "value61", "value62", "value63", "value64", "value65", "value66", "value67", "value68", "value69", "value70", "value71", "value72", "value73", "value74", "value75", "value76", "value77", "value78", "value79", "value80", "value81", "value82", "value83", "value84", "value85", "value86", "value87", "value88", "value89", "value90", "value91", "value92", "value93", "value94", "value95", "value96", "value97", "value98", "value99", "value100", "value101", "value102", "value103", "value104", "value105", "value106", "value107", "value108", "value109", "value110", "value111", "value112", "value113", "value114", "value115", "value116", "value117", "value118", "value119", "value120", "value121", "value122", "value123", "value124", "value125", "value126", "value127", "value128", "value129", "value130", "value131", "value132", "value133", "value134", "value135", "value136", "value137", "value138", "value139", "value140", "value141", "value142", "value143", "value144", "value145", "value146", "value147", "value148", "value149", "value150", "value151", "value152", "value153", "value154", "value155", "value156", "value157", "value158", "value159", "value160", "value161", "value162", "value163", "value164", "value165", "value166", "value167", "value168", "value169", "value170", "value171", "value172", "value173", "value174", "value175", "value176", "value177", "value178", "value179", "value180", "value181", "value182", "value183", "value184", "value185", "value186", "value187", "value188", "value189", "value190", "value191", "value192", "value193", "value194", "value195", "value196", "value197", "value198", "value199", "value200", "value201", "value202", "value203", "value204", "value205", "value206", "value207", "value208", "value209", "value210", "value211", "value212", "value213", "value214", "value215", "value216", "value217", "value218", "value219", "value220", "value221", "value222", "value223", "value224", "value225", "value226", "value227", "value228", "value229", "value230", "value231", "value232", "value233", "value234", "value235", "value236", "value237", "value238", "value239", "value240", "value241", "value242", "value243", "value244", "value245", "value246", "value247", "value248", "value249", "value250", "value251", "value252", "value253", "value254", "value255", "value256", "value257", "value258", "value259", "value260", "value261", "value262", "value263", "value264", "value265", "value266", "value267", "value268", "value269", "value270", "value271", "value272", "value273", "value274", "value275", "value276", "value277", "value278", "value279", "value280", "value281", "value282", "value283", "value284", "value285", "value286", "value287", "value288", "value289", "value290", "value291", "value292", "value293", "value294", "value295", "value296", "value297", "value298", "value299", "value300", "value301", "value302", "value303", "value304", "value305", "value306", "value307", "value308", "value309", "value310", "value311", "value312", "value313", "value314", "value315", "value316", "value317", "value318", "value319", "value320", "value321", "value322", "value323", "value324", "value325", "value326", "value327", "value328", "value329", "value330", "value331", "value332", "value333", "value334", "value335", "value336", "value337", "value338", "value339", "value340", "value341", "value342", "value343", "value344", "value345", "value346", "value347", "value348", "value349",
"value350")

Anyway, it is good practice to avoid long lines to increase code readability. Stick to 80 or 120 character long lines, e.g.:

vector2 <- c("value1", "value2", "value3", "value4", "value5", "value6", "value7",
"value8", "value9", "value10", "value11", "value12", "value13",
"value14", "value15", "value16", "value17", "value18", "value19",
.
.
.
"value344", "value345", "value346", "value347", "value348", "value349",
"value350")

Extract all maximum length values in a character vector in R

To measure the "length" of the string you have to use something like nchar. If you want all the elements which have the maximum number of characters you can filter with nchar(a)==max(nchar(a)). The following code should do what you are trying to do:

a <- c("110", "101", "abc", "cab")

a[nchar(a)==max(nchar(a))]
[1] "110" "101" "abc" "cab"

Finding longest length out of 3 different vectors in R

We can place it in a list, use lengths to create an index of maximum length and extract those element from the list

lst[which.max(lengths(lst))]

data

lst <- list(x, y, z)

Length of the longest element in a list

We get the length of the individual elements of the list with lengths, then get the max value of it with max, create a logical index and subset the list

i1 <-  max(lengths(lst))
i2 <- lengths(lst)== i1
lst[i2]

Assuming the OP wanted the vector as described in the post

f1 <- function(listA, i = 1) {
i1 <- max(lengths(listA))
listB <- lapply(listA, `length<-`, i1)
Reduce(`+`, lapply(listB, function(x) replace(x, is.na(x), 0)))


}

f1(lst)
#[1] 3 -36 54

If we need it in a matrix, it can be done with stri_list2matrix and get the sum with rowSums

library(stringi)
out <- stri_list2matrix(lst)
class(out) <- 'numeric'
rowSums(out, na.rm = TRUE)
#[1] 3 -36 54

data

lst <- list(-3, c(0, 0), c(6, -36, 54))

R: How to find max length sequence between two values in a vector?

We could use rle. A==0 output a logical index vector, rle computes the lengths and runs of values of adjacent elements that are the same for logical vector. Extract the lengths of values that are not '0' and get the max after removing the first and last elements to account for the maximum lengths of non-zero elements at the start or end of vector.

 max(with(rle(A==0), lengths[-c(1, length(lengths))][
!values[-c(1, length(values))]]))
#[1] 2

Another example

   A1 <- c(1, 1, 1, 1, 1, 0, 0, 1, 1, 0, 0,0, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1)
max(with(rle(A1==0), lengths[-c(1, length(lengths))][
!values[-c(1, length(values))]]))
#[1] 4

Or

 indx <- A1==0
max(with(rle(A1[which(indx)[1L] : tail(which(indx),1)]==0),
lengths[!values]))
#[1] 4

Update

Based on the new info, may be you can try,

 A1 <- c(1, 1, 0,1,1,1)  
max(with(rle(A1==0), lengths[!values]))
#[1] 3

maximum size of a matrix in R

The theoretical limit of a vector in R is 2147483647 elements. So that's about 1 billion rows / 2 columns.

...but that amount of data does not fit in 4 GB of memory... And especially not with strings in a character vector. Each string is at least 96 bytes (object.size('a') == 96), and each element in your matrix will be a pointer (8 bytes) to such a string (there is only one instance of each unique string though).

So what typically happens is that the machine starts using virtual memory and start swapping. Heavy swapping typically kills all hope of ever finishing in this century - especially on Windows.

But if you are using a package (igraph?) and you're asking it to produce the matrix, it probably does a lot of internal work and creates lots of auxiliary objects. So even if you're nowhere near the memory limit for the single result matrix, the algorithm used to produce it can run out of memory. It can also be non-linear (quadratic or worse) in time, which would again kill all hope of ever finishing in this century...

A good way to investigate could be to time it on a small graph (e.g. using system.time), and the again when doubling the graph size a couple of times. Then you can see if the time is linear or quadratic and you can estimate how long it will take to complete your big graph. If the prediction says a week, well then you know ;-)



Related Topics



Leave a reply



Submit