apply

R Create new column of values based on the factor levels of another column [duplicate]

倾然丶 夕夏残阳落幕 提交于 2021-01-29 17:14:53
问题 This question already has answers here : R ifelse to replace values in a column (3 answers) Closed 11 months ago . I am trying to create a new column of values based on the values of another column. If the values in column iucnStatus are "LC" or "NT" I want the value in the new column (threatened) to be "Not_Threatened". If the values in iucnStatus are "VU", "EN", "CR", "EW", or "EX" I want the value in the new column (threatened) to be "Threatened." I got this far in writing code to create

how to use apply family instead of nested for loop for my problem

守給你的承諾、 提交于 2021-01-29 16:34:39
问题 I want to fill a new data frame called hd5 based on a conditions from a old data frame called dfnew1 . Can I do it without a nested for loop ? for( j in 2 : length(hd6) ) { for( i in 1: length(hd5$DATE) ) { abcd= dfnew1 %>% filter( (Date == hd5$DATE[i]) , (StrikePrice== hd6[j]) , (OptionType== "CE")) %>% arrange( dte ) hd5[i,j]= abcd[1,9] } } hd6= [13900,14000,14100,14200] dfnew1 looks like this Date expiry optiontype strikeprice closeprice dte 1/1/2019 31/1/2019 ce 13900 700 30 1/1/2019 31/1

Using Apply or Vectorize to apply custom function to a dataframe

左心房为你撑大大i 提交于 2021-01-29 09:14:26
问题 I am attempting to apply a custom function that calls components of that dataframe to do a calculation. I have made a trivial example below because my actual problem is very hard to make a reproducible example. In the below example I want to have the first two columns be added together to create a third column which is the sum of them. Below is an example I found online that gets close to what I want: celebrities=data.frame(name=c("Andrew","matt","Dany","Philip","John","bing","Monica"), age=c

how can I use rollapply with scale

时光毁灭记忆、已成空白 提交于 2021-01-29 08:13:44
问题 I have a data which can be generated like this: set.seed(1) foo <- sample(1:10000,1000) foo[c(1:100)] <- 1 After this to get the zvalues, which are calculated by scale, I used: boo<-rollapply(foo,50,scale) But all the values of boo seems to be NAN. background info: z-score = scale = (x - mean)/ std deviation My first question is why do I get NAN for all the values? For the first 100, I understand that std dev is o . So, I should get Nan only for the first few rows, but I get NAN for all the

How to apply a traversable of functions to a one value

放肆的年华 提交于 2021-01-28 17:49:28
问题 What should I use if I want to have something like [a->b] -> a -> [b] basically I have a list of functions, all take in a value a and returns b . I want to apply all of them to one a and get the results [b] . Which one should I use? Thanks 回答1: You don't need Traversable , just Functor : swingMap f x = fmap ($ x) f See also the swing function (this is equivalent to swing fmap ). Or, if you're using Edward Kmett's distributive library, you can have the best of both this answer (only a Functor

replace a nested for loop with mapply

断了今生、忘了曾经 提交于 2021-01-28 08:24:45
问题 I am a beginner in R. I am working on linear programming using R studio's Cplex to solve a model. One of the constraint in my model is Xl(i,j,t) <= D(i,j,t). I am able to do this with nested for loop with a small dimension (16X16X6). But I want to run my model a lot bigger model, more like 2500X2500X60. I need to save memory and run it faster than the nested for loop. I thought about using apply but I don't know how to make it work. Any help would be greatly appreciated! location <-16 horizon

Apply FUN row-wise on data frame with integer and character variables

白昼怎懂夜的黑 提交于 2021-01-28 06:07:51
问题 A completely basic question - and forgive me if it is a duplicate. set.seed(1) df <- data.frame(id=c('a', 'a', 'b', 'b', 'a'), a=sample(1:10, size=5, replace=T), b=sample(1:10, size=5, replace=T), c=sample(1:10, size=5, replace=T)) Then, > df id a b c 1 a 3 9 3 2 a 4 10 2 3 b 6 7 7 4 b 10 7 4 5 a 3 1 8 To return the column name (a, b or c) with the largest value, and if this is in the id variable take the second highest, I use the below function. FUN <- function(r) { top <- names(r[,c('a', 'b

apply-strsplit-rowwise including sort and nested paste

£可爱£侵袭症+ 提交于 2021-01-28 05:36:01
问题 I guess I just don't see it, but all the similar thing I found on the Net, in the Mailinglist archives or the FAQ could not really elucidate my issue. The closest I have found was this: apply strsplit rowwise I have a df, with two character columns and one numerical column. Filled like this: df=data.frame(name1=c("A","B","C","D"), name2=c("B","A","D","C"), nums=c(1,1,4,4), stringsAsFactors=F) Now I would like to find the unique rows in this, however, only based on the two name columns. And

How to proxy JavaScript creation primitive

江枫思渺然 提交于 2021-01-28 05:16:28
问题 I want to do something when creating a string, for example: String = new Proxy(String, { construct: (t, a) => { return { a: 123 } } }) console.log(new String('q')) // { a: 123 } However, if you use primitives, it doesn't work. String = new Proxy(String, { construct: (t, a) => { return { a: 123 } } }) console.log('1') // Expected: { a: 123 }, Actual: 1 Is there any way? then the second question is, when the runtime converts primitives, can I proxy the process? var a = '123' // This is a

Applying multiple function via sapply

一个人想着一个人 提交于 2021-01-28 03:55:24
问题 I'm trying to replicate solution on applying multiple functions in sapply posted on R-Bloggers but I can't get it to work in the desired manner. I'm working with a simple data set, similar to the one generated below: require(datasets) crs_mat <- cor(mtcars) # Triangle function get_upper_tri <- function(cormat){ cormat[lower.tri(cormat)] <- NA return(cormat) } require(reshape2) crs_mat <- melt(get_upper_tri(crs_mat)) I would like to replace some text values across columns Var1 and Var2 . The