## How to apply the result of a function to factors of a data.frame (R)?

lapply

apply function in r

sapply

dataframe in r

convert categorical variable to factor in r

subset in r

create dataframe in r

I have a function (la) that I would like to apply to some data

la<-function(x) { res<-mean(x)^2 return(c(res,res^2)) } a<-c(1,3,5,6) b<-c(5,8,9,10) df<-data.frame(a,b) colnames(df)<-c('tata','toto') all<-stack(df)

> aggregate(all$values, by=list(all$ind), FUN=la) Group.1 x.1 x.2 1 tata 14.0625 197.7539 2 toto 64.0000 4096.0000

and I get the values for a :

> aggregate(all$values, by=list(all$ind), FUN=la)[,2][,1] [1] 14.0625 64.0000

My question: Is it possible to create a new column to the data.frame 'all', where the third column will be the first column multiplied by 14.0625 and 64.0000 (depending on the factor) ?

Many thanks

Using your custom function and aggregate code you could do

la<-function(x) { res<-mean(x)^2 return(c(res,res^2)) } agg <- aggregate(all$values, by=list(all$ind), FUN=la) mult <- setNames(agg[,2][,1], agg[[1]]) all$all <- all$values*mult[all$ind] all # values ind all # 1 1 tata 14.0625 # 2 3 tata 42.1875 # 3 5 tata 70.3125 # 4 6 tata 84.3750 # 5 5 toto 320.0000 # 6 8 toto 512.0000 # 7 9 toto 576.0000 # 8 10 toto 640.000

However, if this is all you're trying to accomplish you don't need the custom function and aggregate, and can use the code below instead.

library(dplyr) all %>% group_by(ind) %>% mutate(all = values*mean(values)^2) # # A tibble: 8 x 3 # # Groups: ind [2] # values ind all # <dbl> <fct> <dbl> # 1 1 tata 14.1 # 2 3 tata 42.2 # 3 5 tata 70.3 # 4 6 tata 84.4 # 5 5 toto 320 # 6 8 toto 512 # 7 9 toto 576 # 8 10 toto 640

Or with data.table

library(data.table) setDT(all) all[, all := values*mean(values)^2, by = ind] all # values ind all # 1: 1 tata 14.0625 # 2: 3 tata 42.1875 # 3: 5 tata 70.3125 # 4: 6 tata 84.3750 # 5: 5 toto 320.0000 # 6: 8 toto 512.0000 # 7: 9 toto 576.0000 # 8: 10 toto 640.0000

**R tutorial on the Apply family of functions,** In this tutorial, you'll learn about the use of the apply functions in R, of the data that you want to operate on and the format of the output that When you apply it to a vector or a factor x , the function replicates its The critical difference is the use of the by clause, which sets the variable or dataframe field by� Apply functions in R. employee <- as.data.frame(employee.mat) employee result <- apply column 2 is a factor object and FUN is for the function to be performed.

You may use `ave`

.

all$newcol1 <- with(all, ave(values, ind, FUN=function(x) x * la(x))) all # values ind newcol # 1 1 tata 14.0625 # 2 3 tata 42.1875 # 3 5 tata 70.3125 # 4 6 tata 84.3750 # 5 5 toto 320.0000 # 6 8 toto 512.0000 # 7 9 toto 576.0000 # 8 10 toto 640.0000

**15 Easy Solutions To Your Data Frame Problems In R,** Discover how to create a data frame in R, change column and row names, access Also, note that when you use the data.frame() function, character variables Of.Death variables of writers_df have all been read in as factors. such as read.csv() and read.delim() , you'll get back a data frame as the result. The apply() collection is bundled with r essential package if you install R with Anaconda. The apply() function can be feed with many functions to perform redundant application on a collection of object (data frame, list, vector, etc.). The purpose of apply() is primarily to avoid explicit uses of loop constructs. They can be used for an input

You already have an answer, but another (more cumbersome) solution:

la<-function(x) { res<-mean(x)^2 return(c(res,res^2)) } a<-c(1,3,5,6) b<-c(5,8,9,10) df<-data.frame(a,b) colnames(df)<-c('tata','toto') all<-stack(df) all_2 <- merge( all, aggregate(all$values, by=list(all$ind), FUN=la), by.x = 'ind', by.y = 'Group.1') all_2$newvalue <- all_2$values * all_2$x[,1] all_2 <- all_2[,c(1,2,4)] # > all_2 # ind values newvalue # 1 tata 1 14.0625 # 2 tata 3 42.1875 # 3 tata 5 70.3125 # 4 tata 6 84.3750 # 5 toto 5 320.0000 # 6 toto 8 512.0000 # 7 toto 9 576.0000 # 8 toto 10 640.0000

**Understanding Factors – Programming with R,** Understand how to represent categorical data in R. You can check this by using the function levels() , and check the number of levels using nlevels() : The recommended way is to use the integer vector to index the factor levels: ' data.frame': 100 obs. of 9 variables: $ ID : Factor w/ 100 levels "Sub001","Sub002 ",..: 1 2 3� Well sapply is a wrapper for lapply.I rarely use sapply but I know it tries to simplify things. It may be that when your function returns a data frame of different classes trying to sapply is trying to force things as a matrix or vector and then the classes get messed up.

**Apply a Function to a Data Frame Split by Factors,** Function by is an object-oriented wrapper for tapply applied to data frames. data. an R object, normally a data frame, possibly a matrix. INDICES. a factor or a list of factors, each of An object of class "by" , giving the results for each subset. Factors are also created when we read non-numerical columns into a data frame. By default, data.frame () function converts character vector into factor. To suppress this behavior, we have to pass the argument stringsAsFactors = FALSE. How to access compoments of a factor?

**Chapter 4: apply Functions,** Apply functions are a family of functions in base R which allow you to The apply functions that this chapter will address are apply, lapply, sapply, vapply, tapply, If you want to specify the type of result you are expecting, use vapply. Dataset t will be created by adding a factor to matrix m and converting it to a dataframe. Apply a lambda function to each row: Now, to apply this lambda function to each row in dataframe, pass the lambda function as first argument and also pass axis=1 as second argument in Dataframe.apply () with above created dataframe object i.e. # Apply a lambda function to each row by adding 5 to each value in each column

**Coerce multiple columns to factors at once,** r dataframe r-factor to be data[,cols] <- lapply(data[,cols], factor) (with the leading comma for columns)? The %<>% operator from magrittr update the lhs object with the resulting cols) str(data) #'data.frame': 4 obs. of 10 variables: # $ A: Factor w/ 4 levels The more recent tidyverse way is to use the mutate_at function: This is an important idiom for writing code in R, and it usually goes by the name Split, Apply, and Combine (SAC). In this case, you split a vector into groups, apply a function to each group, and then combine the result into a vector. Of course, using the with() function, you can write your line of code in a slightly more readable way:

##### Comments

- Thanks ! This is just what needed.
- @2WFR If this solution worked for you, please don't forget to mark it as the accepted answer so others can more easily find it in the future.
- Thanks but this is not exactly what I would like to do. The new column should be the first MULTIPLIED by 14.0625 for the factor tata and 64.0000 for the factor toto, respectively