R list get first item of each element
sapply(d, "[[", 1)
should do the trick.
A bit of explanation:
sapply
: iterates over the elements in the list[[
: is the subset function. So we are asking sapply to use the subset function on each list element.
1 : is an argument passed to "[["
It turns out that "[" or "[[" can be called in a traditional manner which may help to illustrate the point:
x <- 10:1
"["(x, 3)
# [1] 8
Select first element of nested list
Not much of a shortcut, but you can do this:
lapply(x, `[[`, 1)
# [[1]]
# [1] 1
#
# [[2]]
# [1] 3
#
# [[3]]
# [1] 5
Extract the first item of the list from nested lists
Try :
dt$col2 <-sapply(dt$col1 , "[", 1)
(The elements of your lists are not lists, just vectors, so one bracket is fine)
EDIT :
For a column inside a data.table :
dt$col2 <- sapply(dt$col1,function(x){sapply(x,'[',1)})
extracting first value from a list
You need to subset the list, and then the vector in the list:
M[[1]][1]
In other words, M
is a list of 1 element, a character vector of length 24.
You may want to use unlist
M
to convert it to just a vector.
M <- unlist(M)
Then you can just use M[1]
.
To remove the \"
you can use sub
:
sub("\"","",M[1])
[1] " 0.0337302"
Extract the first element for every vector in a list in R
You can check for number of rows in the matrix :
get_vec <- function(Data, n) {
sapply(Data, function(x) if(nrow(x) >= n) x[n, 1] else NA)
}
vector1 <- get_vec(Data$Position, 1)
vector2 <- get_vec(Data$Position, 2)
Get access to the first row of a list in R
A=list()
A[[1]]=c(1,2)
A[[2]]=c(3,4)
A[[3]]=c(5,6)
A
# [[1]]
# [1] 1 2
# [[2]]
# [1] 3 4
# [[3]]
# [1] 5 6
I. First Solution using sapply()
function for just first elements
sapply(A,'[[',1)
# [1] 1 3 5
For Getting lets say 1st, 3rd, 4th elements of each nested list. Not applicable in this example
sapply(A,`[`,c(1,3,4))
II. Second Solution using for
loop
for(i in 1:length(A)){
print (A[[i]][1])
}
# [1] 1
# [1] 3
# [1] 5
How to get the nth element of each item of a list, which is itself a vector of unknown length
We can create a function using sapply
fun1 <- function(lst, n){
sapply(lst, `[`, n)
}
fun1(l, 1)
#[1] 1 3 5 6
fun1(l, 2)
#[1] 2 4 NA 7
Extract first Element of List in List
An option would be to use pluck
library(purrr)
map(lang_codes, pluck, 1)
Or in base R
with lapply
sapply(lang_codes, `[[`, 1)
#[1] "en" "fr"
R - Filter List Using First Element of Each Item in the List
An option is keep
to loop over the list
of vector
s and create a logical vector of length 1 with str_detect
wrapped with any
. Here the pattern checked is the character "K" or (|
) "1" from the start (^
) of the string
library(purrr)
library(stringr)
keep(l_vectors, ~ any(str_detect(.x, "^(K|1)")))
#[[1]]
#[1] "K" "10" "20"
#[[2]]
#[1] "1" "30" "40"
If we check only the first element, no need for any
to be wrapped
keep(l_vectors, ~ str_detect(.x[1], "^(K|1)"))
#[[1]]
#[1] "K" "10" "20"
#[[2]]
#[1] "1" "30" "40"
If it is a fixed match, then as in @markus post, can use %in%
keep(l_vectors, ~ .x[1] %in% c("K", "1"))
Related Topics
Change Color of Only One Bar in Ggplot
Center-Align Legend Title and Legend Keys in Ggplot2 for Long Legend Titles
Writing to a Dataframe from a For-Loop in R
Creating a Facet_Wrap Plot with Ggplot2 with Different Annotations in Each Plot
Dynamically Converting a List of Excel Files to CSV Files in R
Is There an R Markdown Equivalent to \Sexpr{} in Sweave
R 3.4.1 "Single Candle" Personal Library Path Error: Unable to Create 'Na'
Reversed Order After Coord_Flip in R
How to Refer to a Variable Name with Spaces
R Ggplot Barplot; Fill Based on Two Separate Variables
Ggplot2: Line Connecting the Means of Grouped Data
R - Return Position of Element in Matrix
Read.CSV Is Extremely Slow in Reading CSV Files with Large Numbers of Columns
Fast Linear Regression by Group
R Shiny Mouseover Text for Table Columns