"sapply(name, "[", 2)", what's the "[" means? [duplicate] - r

This question already has answers here:
The difference between bracket [ ] and double bracket [[ ]] for accessing the elements of a list or dataframe
(11 answers)
Using '[' square bracket as a function for lapply in R
(2 answers)
Closed 3 years ago.
name
[[1]]
[1] "John" "Davis"
[[2]]
[1] "Angela" "Williams"
[[3]]
[1] "Bullwinkle" "Moose"
The data is as above, I want to take last and first name from the list. The code is:
lastname <- sapply(name, "[", 2)
My question: what does the [ mean?

It is ?Extraction operator. Here, it extracts the 2nd element of the list.
sapply(name, `[`, 2)
In the OP's post, the list elements are vectors. So, it checks the 2nd element and extract that element and output as a vector (sapply)

Related

Delete everything after second comma from string [duplicate]

This question already has answers here:
How to delete everything after nth delimiter in R?
(2 answers)
Closed 3 years ago.
I would like to remove anything after the second comma in a string -including the second comma-. Here is an example:
x <- 'Day,Bobby,Jean,Gav'
gsub("(.*),.*", "\\1", x)
and it gives:
[1] "Day, Bobby, Jean"
while I want:
[1] "Day, Bobby
regardless of the number of names that may exist in x
Use
> x <- 'Day, Bobby, Jean, Gav'
> sub("^([^,]*,[^,]*),.*", "\\1", x)
[1] "Day, Bobby"
The ^([^,]*,[^,]*),.* pattern matches
^ - start of string
([^,]*,[^,]*) - Group 1: 0+ non-commas, a comma, and 0+ non-commas
,.* - a comma and the rest of the string.
The \1 in the replacement pattern will keep Group 1 value in the result.
We can also use strsplit and then paste
toString(head(strsplit(x, ",")[[1]], 2))
#[1] "Day, Bobby"

Lists: First Name Always Quoted [duplicate]

This question already has answers here:
R lists put name of first element in back-ticks [duplicate]
(1 answer)
What leads the first element of a printed list to be enclosed with backticks in R v3.5.1?
(2 answers)
Closed 4 years ago.
Consider the two lists
> list('a' = 1, 'b' = 2)
$`a`
[1] 1
$b
[1] 2
> list( z = 0, a = 1, b = 2) # added space just in case
$`z`
[1] 0
$a
[1] 1
$b
[1] 2
Why is the name of the first (and only the first) element quoted?
I found a similar issue on this SO question but it didn't explain (or at least I failed to understand) why the above occurs.
PS
When accessing list elements by names using the $ notation the problem does not appear, i.e. l1$a works just as well as l1$'a' (with " ` " instead of " ' ").

Multidimensional list - Select second item if there are 2 items [duplicate]

This question already has answers here:
How to get last subelement of every element of a list?
(2 answers)
Closed 4 years ago.
if my question sounds confusing. Here is the example.
n = c('dog')
s = c('dog', 'cat')
b = c('dog', 'cat')
v = c('dog')
x = list(n, s, b, v)
The output of the above code would be
1.'dog'
2.'dog' 'cat'
3.'dog' 'cat'
4.'dog'
My goal is to get the second item when there is 2 items and get the first item when there is 1 item.
I have tried using sapply()
I can get a second element using this code.
sapply(x, "[", 2)
However, i stuck on how to write a condition above.
My desire output would be.
1.'dog'
2.'cat'
3.'cat'
4.'dog'
Please advise, thank you.
We can use tail to extract the last element
sapply(x, tail, 1)
#[1] "dog" "cat" "cat" "dog"
Or using tidyverse
library(tidyverse)
map_chr(x, last)
#[1] "dog" "cat" "cat" "dog"

how to split a sequence in R into multiple sub parts [duplicate]

This question already has answers here:
Chopping a string into a vector of fixed width character elements
(13 answers)
Closed 6 years ago.
seq="GAGTAGGAGGAG",how to split this sequence into the following sub sequence "GAG","TAG","GAG","GAG"i.e how to split the sequence in groups of threes
We can create a function called fixed_split that will split a character string into equal parts. The regular expression is a lookbehind that matches on n elements together:
fixed_split <- function(text, n) {
strsplit(text, paste0("(?<=.{",n,"})"), perl=TRUE)
}
fixed_split("GAGTAGGAGGAG", 3)
[[1]]
[1] "GAG" "TAG" "GAG" "GAG"
Edit
In your comment you say sequence ="ATGATGATG" does not work:
strsplit(sequence,"(?<=.{3})", perl=TRUE)
[[1]]
[1] "ATG" "ATG" "ATG"

Why does class(answers.list[1]) return list in R [duplicate]

This question already has answers here:
The difference between bracket [ ] and double bracket [[ ]] for accessing the elements of a list or dataframe
(11 answers)
Closed 6 years ago.
Given
RAnswers <- c(0:10,NA) ;
SAnswers <- c(0:20,NA) ;
WAnswers <- c(0:30,NA) ;
answers.list <- list(RAnswers,SAnswers,WAnswers) ;
class(RAnswers) ;
class(answers.list) ;
class(answers.list[1]) ;
class(answers.list[[1]]) ;
the results are
> class(RAnswers) ;
[1] "integer"
> class(answers.list) ;
[1] "list"
> class(answers.list[1]) ;
[1] "list"
> class(answers.list[[1]]) ;
[1] "integer"
why does class(answers.list[1]) return "list" ?
In lists, [ is used to subset the list. Because of this, [ returns a list of a different length. The [[ function is used to extract elements or items from the list.
Subsetting often returns an object of the same class in R, although matrices can move to a simpler class with [M,N] subsetting when either M or N is some expression that results in a vector of length 1 (also true for data.frames where N results in a vector of length 1).

Resources