apply

lapply() output as a dataframe of multiple functions - R

China☆狼群 提交于 2019-12-11 00:47:08
问题 I have been trying to create a new dataframe from several computations with lapply() . I have reached this so far reading several questions (1, 2, 3): lapply(mtcars, function(x) c(colnames(x), NROW(unique(x)), sum(is.na(x)), round(sum(is.na(x))/NROW(x),2) ) ) However, colnames(x) doesn't give the colname as x it's a vector. Second, I can't figure out a way to transform this output into a dataframe: lapply(mtcars, function(x) data.frame(NROW(unique(x)), # if I put colnames(x) here it gives an

Using dplyr and conditional formatting to construct an ordinary differential equation from strings

末鹿安然 提交于 2019-12-11 00:23:04
问题 I am trying to create is a system of equations for a specific variable using dplyr and prod from a dataframe of strings to be used in an ordinary differential solver in R ( deSolve ). The location of the variable dictates the form of the equation and therefore I am using grep , filter_at , mutate_at , and apply . Constructing the equation depends on the column of the string/variable of interest based off the following i. If a variable is ever found as a product (P1,P2,P3) then multiply: +1 *

Hash each row of pandas dataframe column using apply

。_饼干妹妹 提交于 2019-12-11 00:15:18
问题 I'm trying to hash each value of a python 3.6 pandas dataframe column with the following algorithm on the dataframe-column ORIG: HK_ORIG = base64.b64encode(hashlib.sha1(str(df.ORIG).encode("UTF-8")).digest()) However, the above mentioned code does not hash each value of the column, so, in order to hash each value of the df-column ORIG, I need to use the apply function. Unfortunatelly, I don't seem to be good enough to get this done. I imagine it to look like the following code: df["HK_ORIG"]

Pandas apply, but access previously calculated value

偶尔善良 提交于 2019-12-10 21:43:28
问题 Suppose I have a DataFrame (or Series) like this: Value 0 0.5 1 0.8 2 -0.2 3 None 4 None 5 None I wish to create a new Result column. The value of each result is determined by the previous Value, via an arbitrary function f . If the previous Value is not available (None or NaN), I wish to use instead the previous Result (and apply f to it, of course). Using the previous Value is easy, I just need to use shift . However, accessing the previous result doesn't seem to be that simple. For example

Getting rows of a matrix which coincide with a series of vectors, without using apply

让人想犯罪 __ 提交于 2019-12-10 21:07:41
问题 My question is sort of related to my earlier question. Suppose I have one matrix and 4 vectors (can consider this another matrix, since the order of the vectors matters), and I want to get the row numbers which coincide to each vector, in order. I would like the solution to avoid repeating vectors and be as efficient as possible, since the problem is large scale. Example. set.seed(1) M = matrix(rpois(50,5),5,10) v1 = c(3, 2, 7, 7, 4, 4, 7, 4, 5, 6) v2= c(8, 6, 4, 4, 3, 8, 3, 6, 5, 6) v3= c(4,

Apply and lambdas in Scala

寵の児 提交于 2019-12-10 19:35:08
问题 I have the following piece of code scala> val builder = new StringBuilder("foo bar baz ") builder: StringBuilder = foo bar baz scala> (0 until 5) foreach { builder.append("!") } scala> builder.toString res15: String = foo bar baz ! I guess that in reality something like (0 until 5) foreach builder.append("!").apply happens. Can anyone explain to me why that happens or if I'm wrong what really happens? 回答1: To understand what's happening I used my small tool scala-to-java. Your code transpiled

Update Pandas Cells based on Column Values and Other Columns

廉价感情. 提交于 2019-12-10 18:50:58
问题 I am looking to update many columns based on the values in one column; this is easy with a loop but takes far too long for my application when there are many columns and many rows. What is the most elegant way to get the desired counts for each letter? Desired Output: Things count_A count_B count_C count_D ['A','B','C'] 1 1 1 0 ['A','A','A'] 3 0 0 0 ['B','A'] 1 1 0 0 ['D','D'] 0 0 0 2 回答1: The most elegant is definitely the CountVectorizer from sklearn. I'll show you how it works first, then

R - Replace a double loop by a function from the apply family

空扰寡人 提交于 2019-12-10 18:09:38
问题 I have these loops : xall = data.frame() for (k in 1:nrow(VectClasses)) { for (i in 1:nrow(VectIndVar)) { xall[i,k] = sum(VectClasses[k,] == VectIndVar[i,]) } } The data: VectClasses = Data Frame containing the characteristics of each classes VectIndVar = Data Frame containing each record of the data base The two for loops work and give an output I can work with, however, it takes too long, hence my need for the apply family The output I am looking for is as this: V1 V2 V3 V4 1 3 3 2 2 2 2 2

R - apply function - Deactivation of matrix conversion

随声附和 提交于 2019-12-10 18:03:07
问题 Is it possible to deactivate the as.matrix() conversion of apply()? In the R documentation and in previous posts at stack overflow, I could not find any flags to solve this problem. Example: Selection of multiple sub matrices from a matrix using apply(). Problem: The apply() function automatically converts the results to a matrix. This leads to one big matrix containing all results. Code: #mat contains the data, m the desired column selections mat <- matrix(c(1,2,3,4, 2,3,4,1, 2,4,3,1, 3,4,2

Fastest way to convert a list of character vectors to numeric in R

末鹿安然 提交于 2019-12-10 17:55:41
问题 In R , what is the fastest way to convert a list containing suites of character numbers (as character vectors) into numeric? With the following dummy data: set.seed(2) N = 1e7 ncol = 10 myT = formatC(matrix(runif(N), ncol = ncol)) # A matrix converted to characters # Each row is collapsed into a single suite of characters: myT = apply(myT, 1, function(x) paste(x, collapse=' ') ) head(myT) Producing: [1] "0.1849 0.855 0.8272 0.5403 0.3891 0.5184 0.7776 0.5533 0.1566 0.01591" [2] "0.7024 0.1008