sapply

Remove strings found in vector 1, from vector 2

旧巷老猫 提交于 2019-11-26 18:38:42
问题 I have these two vectors: sample1 <- c(".aaa", ".aarp", ".abb", ".abbott", ".abogado") sample2 <- c("try1.aarp", "www.tryagain.aaa", "255.255.255.255", "onemoretry.abb.abogado") I am trying to remove sample1 strings that are found in sample2. The closest I got is by iterating using sapply , which gave me this: sapply(sample1, function(i)gsub(i, "", sample2)) .aaa .aarp .abb .abbott .abogado [1,] "try1.aarp" "try1" "try1.aarp" "try1.aarp" "try1.aarp" [2,] "www.tryagain" "www.tryagain.aaa" "www

Concatenate row-wise across specific columns of dataframe

放肆的年华 提交于 2019-11-26 09:31:11
问题 I have a data frame with columns that, when concatenated (row-wise) as a string, would allow me to partition the data frame into a desired form. > str(data) \'data.frame\': 680420 obs. of 10 variables: $ A : chr \"2011-01-26\" \"2011-01-26\" \"2011-02-09\" \"2011-02-09\" ... $ B : chr \"2011-01-26\" \"2011-01-27\" \"2011-02-09\" \"2011-02-10\" ... $ C : chr \"2011-01-26\" \"2011-01-26\" \"2011-02-09\" \"2011-02-09\" ... $ D : chr \"AAA\" \"AAA\" \"BCB\" \"CCC\" ... $ E : chr \"A00001\" \

Apply a function to every row of a matrix or a data frame

你说的曾经没有我的故事 提交于 2019-11-26 08:55:15
问题 Suppose I have a n by 2 matrix and a function that takes a 2-vector as one of its arguments. I would like to apply the function to each row of the matrix and get a n-vector. How to do this in R? For example, I would like to compute the density of a 2D standard Normal distribution on three points: bivariate.density(x = c(0, 0), mu = c(0, 0), sigma = c(1, 1), rho = 0){ exp(-1/(2*(1-rho^2))*(x[1]^2/sigma[1]^2+x[2]^2/sigma[2]^2-2*rho*x[1]*x[2]/(sigma[1]*sigma[2]))) * 1/(2*pi*sigma[1]*sigma[2]

Create frequency tables for multiple factor columns in R

一曲冷凌霜 提交于 2019-11-26 08:33:40
问题 I am a novice in R. I am compiling a separate manual on the syntax for the common functions/features for my work. My sample dataframe as follows: x.sample <- structure(list(Q9_A = structure(c(5L, 3L, 5L, 3L, 5L, 3L, 1L, 5L, 5L, 5L), .Label = c(\"Impt\", \"Neutral\", \"Not Impt at all\", \"Somewhat Impt\", \"Very Impt\"), class = \"factor\"), Q9_B = structure(c(5L, 5L, 5L, 3L, 5L, 5L, 3L, 5L, 3L, 3L), .Label = c(\"Impt\", \"Neutral\", \"Not Impt at all\", \"Somewhat Impt\", \"Very Impt\"),

Grouping functions (tapply, by, aggregate) and the *apply family

女生的网名这么多〃 提交于 2019-11-25 22:12:20
问题 Whenever I want to do something \"map\"py in R, I usually try to use a function in the apply family. However, I\'ve never quite understood the differences between them -- how { sapply , lapply , etc.} apply the function to the input/grouped input, what the output will look like, or even what the input can be -- so I often just go through them all until I get what I want. Can someone explain how to use which one when? My current (probably incorrect/incomplete) understanding is... sapply(vec, f