Extracting vectors from list in R without looping - r

This is probably a very simple question, however I have googled for hours without a satisfying answer. Let's suppose I have a list like the following one:
theList <- list(c("de", "labore", "solis"), c("sapiento", "post", "eventum"), c("sursum", "corda"))
> theList
[[1]]
[1] "de" "labore" "solis"
[[2]]
[1] "sapiento" "post" "eventum"
[[3]]
[1] "sursum" "corda"
If I want to print all the vectors that compose the list I would think of something like
for(i in 1:length(theList)) {
print(theList[[i]])
}
[1] "de" "labore" "solis"
[1] "sapiento" "post" "eventum"
[1] "sursum" "corda"
however there must be a more elegant solution, probably using some member of the apply family...

I think I got an answer after doing some more googling and experimenting...
extract <- function(m) {
sapply(seq_along(m), function(x) m[[x]])
}
> extract(theList)
[[1]]
[1] "de" "labore" "solis"
[[2]]
[1] "sapiento" "post" "eventum"
[[3]]
[1] "sursum" "corda"
So it would be a matter of performing the desired operation (e.g. printing) on the result from the sapply

Related

Getting "argument is of length 0" inside if statement [duplicate]

I am having a little problem with R and I am not sure why. It is telling me that this line: if(temp > data[[k]][[k2]]) { is of argument length 0. Here is the block which is not that big:
for(k in 1:length(data)) {
temp <- 0
for(k2 in 3:length(data[[k]])) {
print(data[[k]][[k2]])
if(temp > data[[k]][[k2]]) {
temp <- data[[k]][[k2]]
}
fMax[k] <- temp
k2 <- k2 + 1
}
k <- k + 1
}
example of what is in data[[k]][[k2]]:
[1] "3050"
[1] "3051"
[1] "3054"
[1] "3054"
[1] "3052"
[1] "3053"
[1] "3059"
[1] "3059"
[1] "3057"
[1] "3060"
[1] "3063"
[1] "3060"
[1] "3068"
[1] "3067"
[1] "3079"
[1] "3085"
[1] "3094"
[1] "3107"
[1] "3121"
[1] "3135"
[1] "3147"
[1] "3161"
[1] "3200"
[1] "3237"
[1] "3264"
[1] "3274"
[1] "3284"
[1] "3289"
[1] "3292"
[1] "3300"
[1] "3301"
[1] "3303"
[1] "3306"
[1] "3310"
[1] "3312"
[1] "3313"
[1] "3319"
[1] "3314"
[1] "3318"
[1] "3318"
[1] "3320"
[1] "3322"
[1] "3322"
[1] "3322"
[1] "3328"
[1] "3332"
[1] "3338"
[1] "3350"
[1] "3358"
[1] "3378"
[1] "3395"
[1] "3402"
[1] "3875"
[1] "3950"
[1] "3988"
[1] "4018"
[1] "4039"
[1] "4048"
[1] "4057"
[1] "4062"
[1] "4067"
[1] "4076"
[1] "4082"
[1] "4085"
[1] "4092"
[1] "4098"
[1] "4099"
[1] "4101"
[1] "4107"
[1] "4119"
[1] "4139"
[1] "4164"
[1] "4231"
[1] "4347"
[1] "4559"
"argument is of length zero" is a very specific problem that comes from one of my least-liked elements of R. Let me demonstrate the problem:
> FALSE == "turnip"
[1] FALSE
> TRUE == "turnip"
[1] FALSE
> NA == "turnip"
[1] NA
> NULL == "turnip"
logical(0)
As you can see, comparisons to a NULL not only don't produce a boolean value, they don't produce a value at all - and control flows tend to expect that a check will produce some kind of output. When they produce a zero-length output... "argument is of length zero".
(I have a very long rant about why this infuriates me so much. It can wait.)
So, my question; what's the output of sum(is.null(data[[k]]))? If it's not 0, you have NULL values embedded in your dataset and will need to either remove the relevant rows, or change the check to
if(!is.null(data[[k]][[k2]]) & temp > data[[k]][[k2]]){
#do stuff
}
Hopefully that helps; it's hard to tell without the entire dataset. If it doesn't help, and the problem is not a NULL value getting in somewhere, I'm afraid I have no idea.
The same error message results not only for null but also for e.g. factor(0). In this case, the query must be if(length(element) > 0 & otherCondition) or better check both cases with if(!is.null(element) & length(element) > 0 & otherCondition).
You can use isTRUE for such cases. isTRUE is the same as { is.logical(x) && length(x) == 1 && !is.na(x) && x }
If you use shiny there you could use isTruthy which covers the following cases:
FALSE
NULL
""
An empty atomic vector
An atomic vector that contains only missing values
A logical vector that contains all FALSE or missing values
An object of class "try-error"
A value that represents an unclicked actionButton()
I spent an entire day bashing my head against this, the solution turned out to be simple..
R isn't zero-index.
Every programming language that I've used before has it's data start at 0, R starts at 1.
The result is an off-by-one error but in the opposite direction of the usual.
going out of bounds on a data structure returns null and comparing null in an if statement gives the argument is of length zero error. The confusion started because the dataset doesn't contain any null, and starting at position [0] like any other pgramming language turned out to be out of bounds.
Perhaps starting at 1 makes more sense to people with no programming experience (the target market for R?) but for a programmer is a real head scratcher if you're unaware of this.
The argument is of length zero takes places when you get an output as an integer of length 0 and not a NULL output.i.e.,
integer(0).
You can further verify my point by finding the class of your output-
>class(output)
"integer"
The simplest solution to the problem is to change your for loop statement :
Instead of using
for (i in **0**:n))
Use
for (i in **1**:n))
In my case, I just wanted to see the first position of the character as follows
htagPos <- which(strsplit(val, "")[[1]] == "#")
if(htagPos == 1){
next
}# this did now work:(
So I had to check the length of the result first before checking the value
htagPos <- which(strsplit(val, "")[[1]] == "#")
if(length(htagPos) >= 1 && htagPos == 1){
next
}
I see why most people prefer python...
So the other possibility for this error can be when the condition in IF is a return value from other function.
For example,
check <- function (value) {
if (value == 0) {
return TRUE
}
Now,
If this function is called like this:
if(check(value)) {
do something
}
So here, let's assume the value is not 0, there is no return statement for that case. In this case too, you'll get "argument is of length zero" error.
Hope this is helpful!
String data structures have the last data addressed nulled so use max(data) instead of data[last].
https://www.geeksforgeeks.org/string-data-structure/
For example,a string with 4 elements will have a number element in it's 5th element.

How do you make a vector of character strings from a vector of objects?

I want to generate a vector of character strings from a vector of objects.
Bob <- c(1,2,3,4)
Anne <- c(3,5,7,1)
Tim <- c(4,2,1,1)
People <- c(Bob, Anne, Tim)
Now what I want is:
> Names
[1] "Bob" "Anne" "Tim"
I know you can do this individually with
> deparse(substitute(Bob))
[1] "Bob"
So I tried to do
Names <- lapply(People,function(k){deparse(substitute(k))})
but this did not give the expected results. It produced a long list where each element is:
[[1]]
[1] "X[[i]]"
[[2]]
[1] "X[[i]]"
...
I'm sure this is a fairly easy task, but I can't get it working. Thanks!

Creating named list of large set of data frames R

This may be a trivial question, given I only have a few days' experience using R but basically i'd like to create a named list of data frames. I've seen a bunch of questions and answers dealing with only 2 data frames e.g
dataList <- list(x=data.frame1, y=data.frame2)
However i'm working with 48 data frames in this program and would like to know a more elegant way of creating a named list of 48 elements without actually explicitly naming all 48 of my data frames (which is what i've currently got):
dataList <- list(forecasted.data0=forecasted.data0, forecasted.data1=forecasted.data1, ...) and so on 46 more times.
Use the names or setNames function
> x <- as.list(rnorm(5))
> x
[[1]]
[1] -1.404512
[[2]]
[1] 0.927126
[[3]]
[1] 1.055555
[[4]]
[1] -1.718295
[[5]]
[1] 0.5154312
> names(x) <- paste0("forecasted.data.", seq(0, length(x) - 1))
> x
$forecasted.data.0
[1] -1.404512
$forecasted.data.1
[1] 0.927126
$forecasted.data.2
[1] 1.055555
$forecasted.data.3
[1] -1.718295
$forecasted.data.4
[1] 0.5154312
Would this work ?
forecasted.data0=data.frame(1)
forecasted.data1=data.frame(2)
n=ls()[grepl("^forecasted.data.*",ls())]
l=sapply(n, function(x) get(x))
names(l)=n
You can replace sapply with lapply if you prefer ...

Why doesn't appending to list via non-existent indexes do what I expect?

I want to achieve the following outcome in a list:
[[1]]
[1] "one"
[[1]]
[2] "two"
I would like to ask why the next way in doing so, by using indexes, fails:
List <- list()
List[[1]][1] <- "one"
List[[1]][2] <- "two"
If we do not use index the first time we append, it works fine:
List <- list()
List[[1]]<- "one"
List[[1]][2]<- "two"
As Richard Scriven noticed correctly,
List[[1]][1]<- "one"
fails because position 1 does not exist. But this is true also for position 2 in the solution above. And this is somehow strange.

String Split into list R

Extract words from a string and make a list in R
str <- "qwerty keyboard"
result <- strsplit(str,"[[:space:]]")
What I get was..(down below)
result
[[1]]
[1] "qwerty" "keyboard"
What I need is..(down below)
result
[[1]]
[1] "qwerty"
[[2]]
[1] "keyboard"
[OR]
result
[[1]]
[1] "qwerty"
[2] "keyboard"
I am looking for a solution, if someone knows please post your solution here.
thanks in advance..
try:
str <- "qwerty keyboard"
result_1 <- strsplit(str,"[[:space:]]")[[1]][1]
result_2 <- strsplit(str,"[[:space:]]")[[1]][2]
result <- list(result_1,result_2)
Or
as.list(strsplit(str, '\\s+')[[1]])
as.list(unlist(strsplit(str, '[[:space:]]')))
As an alternative to strsplit(), you can make a list out of the result from scan().
as.list(scan(text=str, what=""))
# Read 2 items
# [[1]]
# [1] "qwerty"
#
# [[2]]
# [1] "keyboard"

Resources