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 " ' ").
Related
This question already has answers here:
Select list element programmatically using name stored as string [duplicate]
(2 answers)
Closed 2 years ago.
I would like to access an element from a list but I got NULL
> x <- list("b" = TRUE)
> x
$b
[1] TRUE
> x$b
[1] TRUE
> var=c("b","c")
I tried this:
> x$var[1]
NULL
We can use [[ instead of $ as $ would try to literally search for var as the list name instead of the value stored in the object
x[[var[1]]]
#[1] TRUE
This question already has an answer here:
R how to not display the number into brackets of the row count in output
(1 answer)
Closed 2 years ago.
x <- 5+2
print(x)
[1] 7
How to suppress [1] and only print 7?
Similarly for characters:
y <- "comp"
print(y)
[1] "comp"
I want to remove both [1] and " ". Any help is appreciated!
Thanks!
With cat, it is possible
cat(x, '\n')
7
Or for characters
cat(dQuote(letters[1], FALSE), '\n')
"a"
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)
This question already has answers here:
Complete word matching using grepl in R
(3 answers)
Closed 3 years ago.
I would like to use str_extract_all to extract specific text strings from many columns of a spreadsheet containing error descriptions. A sample list:
fire_match <- c('fire', 'burned', 'burnt', 'burn', 'injured', 'injury', 'hurt', 'dangerous',
'accident', 'collided', 'collide', 'crashed', 'crash', 'smolder', 'flame', 'melting',
'melted', 'melt', 'danger')
My code technically does what it is supposed to do, but I am also extracting (for example) 'fire' from 'misfire'. This is incorrect. I am also having difficulty extracting results that are not case sensitive.
This is a direct example of what is getting me 90% of the way there:
fire$Cause.Trigger <- str_extract_all(CAUSE_TEXT, paste(fire_match, collapse="|") )
My desired result is:
CAUSE_TEXT <- c("something caught fire", "something misfired",
"something caught Fire", "Injury occurred")
something caught fire -> fire
something misfired -> N/A
something caught Fire -> fire
an Injury occurred -> injury
You can just add \b to your individial terms to make sure they match a word boundry.
pattern <- paste0("\\b", paste(fire_match , collapse="\\b|\\b"), "\\b")
str_extract_all(CAUSE_TEXT, regex(pattern, ignore_case = TRUE))
# [[1]]
# [1] "fire"
# [[2]]
# character(0)
# [[3]]
# [1] "Fire"
# [[4]]
# [1] "Injury"
This question already has answers here:
How do I strip dollar signs ($) from data/ escape special characters in R?
(4 answers)
Closed 7 years ago.
> str = "a$b$c"
> astr <- strsplit(str,"$")
> astr
[[1]]
[1] "a$b$c"
Still trying to figure the answer out!
You need to escape it
strsplit(str,"\\$")
Another option is to use , fixed = TRUE option:
strsplit(str,"$",fixed=TRUE)
## [1] "a" "b" "c"