apply

Unexpected conversion to chars instead of factors in data frames and matrices

非 Y 不嫁゛ 提交于 2019-12-13 21:28:29
问题 I am not a novice user of R, but the following is most confusing. I have a data frame (although the problem is equally present for matrices) of categorical variables taking the values +1/-1, which I'd like to convert into factors. mat <- matrix(sample(c(-1, +1), 16, replace = T), nrow = 4) mat <- data.frame(mat) However, using mat <- apply(mat, 2, factor) turns integers into characters instead of factors: > mat [,1] [,2] [,3] [,4] [1,] "-1" "1" "-1" "1" [2,] "-1" "-1" "-1" "-1" [3,] "-1" "1"

Pandas: Memory error when using apply to split single column array into columns

拈花ヽ惹草 提交于 2019-12-13 19:10:26
问题 I am wondering if anybody has a quick fix for a memory error that appears when doing the same thing as in the below example on larger data? Example: import pandas as pd import numpy as np nRows = 2 nCols = 3 df = pd.DataFrame(index=range(nRows ), columns=range(1)) df2 = df.apply(lambda row: [np.random.rand(nCols)], axis=1) df3 = pd.concat(df2.apply(pd.DataFrame, columns=range(nCols)).tolist()) It is when creating df3 I get memory error. The DF's in the example: df 0 0 NaN 1 NaN df2 0 [[0

Elegant way to replace values in pandas.DataFrame from another DataFrame

孤者浪人 提交于 2019-12-13 19:08:21
问题 I have a data frame that I want to replace the values in one column, with values from another dataframe. df = pd.DataFrame({'id1': [1001,1002,1001,1003,1004,1005,1002,1006], 'value1': ["a","b","c","d","e","f","g","h"], 'value3': ["yes","no","yes","no","no","no","yes","no"]}) dfReplace = pd.DataFrame({'id2': [1001,1002], 'value2': ["rep1","rep2"]}) I need to use a groupby with common key and current solution is with a loop. Is there a more elegant (faster) way to do this with .map(apply) etc.

vectorize percentile value of column B of column A (for groups)

谁说胖子不能爱 提交于 2019-12-13 12:59:08
问题 For every pair of src and dest airport cities I want to return a percentile of column a given a value of column b . I can do this manually as such: example df with only 2 pairs of src/dest (I have thousands in my actual df): dt src dest a b 0 2016-01-01 YYZ SFO 548.12 279.28 1 2016-01-01 DFW PDX 111.35 -65.50 2 2016-02-01 YYZ SFO 64.84 342.35 3 2016-02-01 DFW PDX 63.81 61.64 4 2016-03-01 YYZ SFO 614.29 262.83 {'a': {0: 548.12, 1: 111.34999999999999, 2: 64.840000000000003, 3: 63

How do I test for numeric values in a dataframe of characters, and convert those to numeric?

倾然丶 夕夏残阳落幕 提交于 2019-12-13 11:48:28
问题 I have a dataframe somewhat like the following: > theDF ID Ticker INDUSTRY_SECTOR VAR CVAR 1 1 USD CASH 0 0 12 2 ZAR CASH -181412.82055904 -301731.22832191 23 3 BAT SJ EQUITY Financial 61711.951234826 102641.162795691 34 4 HCI SJ EQUITY Financial 1095.16002541256 1821.50290513369 45 5 PSG SJ EQUITY Financial 16498.2192382422 27440.331617902 We can see these are all character columns: > apply(theDF, 2, mode) ID Ticker INDUSTRY_SECTOR VAR CVAR "character" "character" "character" "character"

how paste function working in R? [duplicate]

不打扰是莪最后的温柔 提交于 2019-12-13 10:37:53
问题 This question already has an answer here : multiply multiple column and find sum of each column for multiple values (1 answer) Closed 6 years ago . this is my code here the paste function works for only two combination.i need the same code in a loop for more than two combinations at the same time. i<-2 while (i<=10) { results<-data.frame() results<- t(apply(data,1,function(x) combn(x,i,prod))) comb <- combn(colnames(data),i) colnames(results) <- apply(comb,i,function(x) paste(x[1],x[2])) i<-i

R data subset restructuring [closed]

a 夏天 提交于 2019-12-13 09:47:58
问题 Closed . This question needs to be more focused. It is not currently accepting answers. Want to improve this question? Update the question so it focuses on one problem only by editing this post. Closed 5 years ago . I am fairly new to R/Rstudio and I am still learning how to do certain operations. I have the following data set. For columns I have Operating Region, type of element(CA,OBU), sub-element and Net Revenue. Currently the data is quite big(50 000 rows) and I want to get a summary of

R: extract and paste keyword matches

会有一股神秘感。 提交于 2019-12-13 07:38:06
问题 I am new to R and have been struggling with this one. I want to create a new column, that checks if a set of any of words ("foo", "x", "y") exist in column 'text', then write that value in new column. I have a data frame that looks like this: a-> id text time username 1 "hello x" 10 "me" 2 "foo and y" 5 "you" 3 "nothing" 15 "everyone" 4 "x,y,foo" 0 "know" The correct output should be: a2 -> id text time username keywordtag 1 "hello x" 10 "me" x 2 "foo and y" 5 "you" foo,y 3 "nothing" 15

How to use lapply to put elements of a list into different data frames

故事扮演 提交于 2019-12-13 06:13:48
问题 I have a list of dataframes called db; each data frame has its name. I use: lapply(names(db), function(x)write.csv(db[x], file =paste0(x,'.csv'))) to extract the d.frames and save them into csv files. Now i'm trying to extact from the list the dataframes and create different dataframes with this command: lapply(names(db), function(x)as.data.frame(db[x])) But it does not store any data frame into the workspace , how can I store each df with different names into the workspace? 回答1: We extract

Scheme - using apply

℡╲_俬逩灬. 提交于 2019-12-13 05:25:24
问题 I received the following exercise as homework. I sat on it for hours without any success, so I have no choice but to use your help. Examples: (define m1 (cons "fixNumber" (lambda () 42))) (define m3 (cons "add" (lambda (x y) (+ x y)))) (define myobj (create-obj (list m1 m2 m3))) (myobj "fixNumber" '()) ;; => 42 (myobj "add" '(1 2)) ;; => 3 (myobj "myMethod" '()) ;; => "Error: no such method" 回答1: This should do: (define (create-obj mlist) (lambda (method parms) (let ((func (assoc method mlist