How to use strsplit() to access elements in an R list?
A "tidyverse" approach similar to suggestion in comments by @GavinSimpson:
library(purrr)
library(stringr)
x <- rep("stringA, stringB", 10)
str_split(x, ", ") %>% map_chr(`[`, 2)
#> [1] "stringB" "stringB" "stringB" "stringB" "stringB" "stringB" "stringB"
#> [8] "stringB" "stringB" "stringB"
str_split()
acts likestrsplit()
.map_chr()
acts likelapply()
, but also converts resulting list to a character vector.
For your problem, substitute x
for list1$field2
First entry from string split
If you need to extract the first (or nth
) entry from each split, use:
word <- c('apple-orange-strawberry','chocolate')
sapply(strsplit(word,"-"), `[`, 1)
#[1] "apple" "chocolate"
Or faster and more explictly:
vapply(strsplit(word,"-"), `[`, 1, FUN.VALUE=character(1))
#[1] "apple" "chocolate"
Both bits of code will cope well with selecting whichever value in the split list, and will deal with cases that are outside the range:
vapply(strsplit(word,"-"), `[`, 2, FUN.VALUE=character(1))
#[1] "orange" NA
Access n-th element after string splitting
1) data.frame Convert to a data frame and then it is easy to pick off a column or subset of columns:
DF <- read.table(text = string, sep = ",", as.is = TRUE)
DF[[1]]
## [1] "A" "B" "A"
DF[[3]]
## [1] "some text" "some other text" "yet another one"
DF[-1]
## V2 V3 V4
## 1 1 some text 200
## 2 2 some other text 300
## 3 3 yet another one 100
DF[2:3]
## V2 V3
## 1 1 some text
## 2 2 some other text
## 3 3 yet another one
2) data.table::tranpose The data.table package has a function to tranpose lists so that if stringt
is the tranposed list then stringt[[3]]
is the vector of third fields, say, in a similar way to (1). Even more compact is data.table's tstrsplit
mentioned by @Henrik below or the same package's fread
mentioned by @akrun below.
library(data.table)
stringt <- transpose(strsplit(string, ","))
# or
stringt <- tstrsplit(string, ",")
stringt[[1]]
## [1] "A" "B" "A"
stringt[[3]]
## [1] "some text" "some other text" "yet another one"
stringt[-1]
## [[1]]
## [1] "1" "2" "3"
##
## [[2]]
## [1] "some text" "some other text" "yet another one"
##
## [[3]]
## [1] "200" "300" "100"
stringt[2:3]
## [[1]]
## [1] "1" "2" "3"
##
## [[2]]
## [1] "some text" "some other text" "yet another one"
purrr also has a transpose
function but
library(purrr)
transpose(strsplit(string, ","))
produces a list of lists rather than a list of character vectors.
How to get the first element of a strsplit in R
try this :
variable = unlist(strsplit('AAAAAA.BBBB', '\\.'))[1]
R get last element from str_split
As the comment on your question suggests, this is suitable for gsub
:
gsub("^.*_", "", string_thing)
I'd recommend you take note of the following cases as well.
string_thing <- c("I_AM_STRING", "I_AM_ALSO_STRING_THING", "AM I ONE", "STRING_")
gsub("^.*_", "", string_thing)
[1] "STRING" "THING" "AM I ONE" ""
Getting the second item after using str_split() in R
I think you're looking for str_replace
(or sub
in base R)
x %>% mutate(words = str_replace(question, "^\\d+\\.", ""))
# question words
#1 01. I like my job. I like my job.
#2 02. I like my house. I like my house.
#3 03. I like my car. I like my car.
Explanation:
^
is the left string anchor\\d+\\.
matches one or more digit(s) followed by a full stop
You can use str_split
in the following way
x %>% mutate(words = paste0(map_chr(str_split(question, "\\."), 2), "."))
giving the same result.
How to split an element in a character vector and insert it as a new element?
You are on the right path with strsplit
, just use unlist
to get a vector.
> unlist(strsplit(string, ","))
[1] "Eric" "John" "Dora" "Michael" " James" "Susan"
R split string into elements, sort and recombine
We need a second *apply to loop through elements of x.
test[, (cols) := (lapply(.SD, function(x) {
#browser()
sapply(x, function(y) paste(sort(trimws(strsplit(y, ',')[[1]])), collapse=','))
})), .SDcols = cols]
> test
col1 col2
1: cow,horse,pig aardvark,fish,moose
2: frog,orange,pig aardvark,elk,whale
strsplit(x, ',')[[1]]
used the first element from each column, as you can see below
Browse[1]> strsplit(x, ',')
[[1]]
[1] "cow" " pig" " horse"
[[2]]
[1] "orange" " pig" " frog"
Browse[1]> strsplit(x, ',')[[1]]
[1] "cow" " pig" " horse"
accessing individual values split by str_split in R, finding the last one?
Taken from: Find file name from full file path
basename("C:/some_dir/a")
> [1] "a"
dirname("C:/some_dir/a")
>[1] "C:/some_dir"
Although I think the above approach is much better, you can also use the str_split
approach - which I really only mention to show how to select the last elements from a list using lapply
.
example <- c("C:/some_dir/a","C:/some_dir/sdfs/a","C:/some_dir/asdf/asdf/a")
example.split <- strsplit(example,"/")
files <- unlist(lapply(example.split, tail , 1 ))
Related Topics
Extracting Orthogonal Polynomial Coefficients from R's Poly() Function
How to Access Browser Session/Cookies from Within Shiny App
Rcurl: Http Authentication When Site Responds with Http 401 Code Without Www-Authenticate
Check If a String Contains at Least One Numeric Character in R
Loading Dplyr After Plyr Is Causing Issues
Match Dataframes Excluding Last Non-Na Value and Disregarding Order
Return a List in Dplyr Mutate()
Remove Numbers at the Beginning and End of a String
Intersecting Points and Polygons in R
Retain Attributes When Using Gather from Tidyr (Attributes Are Not Identical)
Weighted Means by Group and Column
R: Saving Ggplot2 Plots in a List
Extracting Common Character Strings from Multiple Vectors of Different Lengths
Ggplot2 One Line Per Each Row Dataframe
What If I Want to Web Scrape with R for a Page with Parameters