Tag: r
-
How do I split a string by a character without ignoring trailing split-characters?
7 I have a string similar to the following my_string <- "apple,banana,orange," And I want to split by , to produce the output: list(c(‘apple’, ‘banana’, ‘orange’, "")) I thought strsplit would accomplish this but it treats the trailing ‘,’ like it doesn’t exist my_string <- "apple,banana,orange," strsplit(my_string, split = ‘,’) #> [[1]] #> [1] "apple"…
-
Fooling R’s Method Dispatch
6 I am looking for an example where a dot in a function name causes R’s Method Dispatch to pick the wrong function. A natural candidate seems to be the builtin function t.test(), which should be confused with t() when applied to an object of class "test". Strangely, however, the following code actually calls the…
-
Take paired differences of pairs of rows
7 Create a new variable that is the difference of two adjacent rows of ‘price’ variable in the data set, where the new variable is the squared difference. test <- data.frame(id = c(6, 16, 26, 36, 46, 56), house = c(1, 5, 10, 23, 25, 27), price = c(79, 84, 36, 34, 21, 12)) where…
-
Efficiently finding all matches of vector in lookup table, with repeats
9 I want to find indices of all matches of a vector x in another lookup vector table. table = rep(1:5, each=3) x = c(2, 5, 2, 6) Standard base R methods don’t quite give me what I want. For example using which(table %in% x) we only get the matching indices once, even though 2…
-
Why is R’s hamming function different from Matlab’s?
8 Why does the hamming function in R give different values to the function of the same name in Matlab? Matlab: hamming(76) 0.0800 0.0816 0.0864 0.0945 0.1056 0.1198 R: library(gsignal) hamming (76) 0.08695652 0.08851577 0.09318288 0.10092597 0.11169213 0.1254078 r function matlab signal-processing Share Improve this question Follow edited 39 mins ago Jacob 1,59888 silver badges2525…
-
From R array to Numpy array
7 Lets say, I have a following R array a <- array(1:18, dim = c(3, 3, 2)) r$> a , , 1 [,1] [,2] [,3] [1,] 1 4 7 [2,] 2 5 8 [3,] 3 6 9 , , 2 [,1] [,2] [,3] [1,] 10 13 16 [2,] 11 14 17 [3,] 12 15 18…
-
How to recursively rename a list based on its list items
7 I’d like to recursively rename (or name, as those items are currently unnamed) a list() based on its items (here text). There are several similar questions, however I haven’t found one with a list structure as follows and I can’t seem to find a general recursive approach to solve this. The example data comes…
-
Student’s t-distribution CDF R base documentation
6 In the context of the Student’s t-distribution cumulative distribution function, R Version 4.3.1’s ?dt documentation highlights the following result: However, upon attempting to verify the accuracy of this formula, an inconsistency arises, as illustrated in the following code snippet: v <- 5 t <- -1 ## Student’s t-distribution cumulative distribution function pt(q = t,…
-
How can I combine two maps in R?
6 I have provided the sample codes below. My issue involves placing ‘map2’ in a square at the top left corner of ‘map1’ and adding an arrow from ‘map2’ to a specific location on ‘map1’. I’ve searched the site, but most commonly discussed topics are related to merging two data layers. library (tidyverse) library (rnaturalearth)…
-
How to combine two maps in R?
6 I need your help, and I have provided the sample codes below. My issue involves placing ‘map2’ in a square at the top left corner of ‘map1’ and adding an arrow from ‘map2’ to a specific location on ‘map1’. I’ve searched the site, but most commonly discussed topics are related to merging two data…