Apply function on each cell in a data.frame
I have a data.frame like this:
G5_01 X8803713069_R01C02_8803713069_R01C02 X8803713069_R02C02_8803713069_R02C02 1: 0/0 0/0 2: 0/0 1/1 3: 0/1 0/0
I want to calculate variations in each cell and convert it to:
X8803713069_R01C02_8803713069_R01C02 X8803713069_R02C02_8803713069_R02C02 1: 0 0 2: 0 2 3: 1 0
data.table seemed to be able to deal with this and my script as shown below:
library(data.table) G5_02<-setDT(G5_01)[,lapply(.SD,function(x) sum(as.numeric(strsplit(x,"/")[]), as.numeric(strsplit(x,"/")[])))]
But it only gives me the result of the first row
X8803713069_R01C02_8803713069_R01C02 X8803713069_R02C02_8803713069_R02C02 1: 0 0
Any suggestions on how should I fix it?
Apply function to each cell in DataFrame in place in pandas, If it matters a lot to you, you can try making your own cpython function. I found the applymap function in pandas def applymap(self, func): # def� DataFrame.apply (func, axis=0, broadcast=None, raw=False, reduce=None, result_type=None, args= (), **kwds) Important Arguments are: func : Function to be applied to each column or row. This function accepts a series and returns a series. axis : Axis along which the function is applied in dataframe. Default value 0.
If you only have to handle 1s and 0s, then a possible solution can be to count the 1s, i.e.
library(data.table) setDT(df)[, lapply(.SD, function(i)stringr::str_count(i, '1'))] # X8803713069_R01C02_8803713069_R01C02 X8803713069_R02C02_8803713069_R02C02 #1: 0 0 #2: 0 2 #3: 1 0
pandas.apply(): Apply a function to each row/column in Dataframe , Important Arguments are: func : Function to be applied to each column or row. This function accepts a series and returns a series. axis : Axis along which the function is applied in dataframe. Default value 0. If value is 0 then it applies function to each column. args : tuple / list of arguments to passed to function. Apply a square root function to every single cell in the whole data frame applymap () applies a function to every single element in the entire dataframe. # Drop the string variable so that applymap () can run df = df.drop('name', axis=1) # Return the square root of every cell in the dataframe df.applymap(np.sqrt)
base R, we can use
rowSums after doing the splitting with
df <- lapply(df, function(x) rowSums(read.table(text = x, sep="/", header = FALSE))) df # X8803713069_R01C02_8803713069_R01C02 X8803713069_R02C02_8803713069_R02C02 #1 0 0 #2 0 2 #3 1 0
df <- structure(list(X8803713069_R01C02_8803713069_R01C02 = c("0/0", "0/0", "0/1"), X8803713069_R02C02_8803713069_R02C02 = c("0/0", "1/1", "0/0")), class = "data.frame", row.names = c(NA, -3L))
Apply a function to every row in a pandas dataframe, You can use .apply to send a single column to a function. This is useful when cleaning up data - converting formats, altering values etc. # What's our data look like? For each Row in an R Data Frame. To call a function for each row in an R data frame, we shall use R apply function. apply ( data_frame, 1, function, arguments_to_function_if_any) The second argument 1 represents rows, if it is 2 then the function would apply on columns. Following is an example R Script to demonstrate how to apply a function for each row in an R Data Frame.
data.table::tstrsplit to get a transposed string-split (like e.g.
purrr::transpose(strsplit(x, '/'))) we can then convert to numeric and add them together
library(dplyr) df %>% mutate_all(~ data.table::tstrsplit(., '/') %>% map(as.numeric) %>% do.call(what = '+')) # X8803713069_R01C02_8803713069_R01C02 X8803713069_R02C02_8803713069_R02C02 # 1 0 0 # 2 0 2 # 3 1 0
pandas.DataFrame.apply — pandas 1.1.0 documentation, Apply a function along an axis of the DataFrame. Objects passed to the function are Series objects whose index is either the DataFrame's index ( axis=0 ) or the� In Example 1, I’ll show you how to perform a function in all rows of a data frame based on the apply function. Let’s assume that our function, which we want to apply to each row, is the sum function. Then, we can use the apply function as follows: apply (data, 1, sum) # apply function # 14 13 14 6 10
library(data.table) setDT(G5_01)[, X8803713069_R01C02_8803713069_R01C02 := as.numeric(substr(X8803713069_R01C02_8803713069_R01C02, 1, 1)) + as.numeric(substr(X8803713069_R01C02_8803713069_R01C02, 3, 3))][, X8803713069_R02C02_8803713069_R02C02 := as.numeric(substr(X8803713069_R02C02_8803713069_R02C02, 1, 1)) + as.numeric(substr(X8803713069_R02C02_8803713069_R02C02, 3, 3))] G5_01 X8803713069_R01C02_8803713069_R01C02 X8803713069_R02C02_8803713069_R02C02 1: 0 0 2: 0 2 3: 1 0
G5_01 <- read.table(text = 'X8803713069_R01C02_8803713069_R01C02 X8803713069_R02C02_8803713069_R02C02 0/0 0/0 0/0 1/1 0/1 0/0', header = T)
pandas.DataFrame.applymap — pandas 1.1.0 documentation, Apply a function to a Dataframe elementwise. This method applies a function that accepts and returns a scalar to every element of a DataFrame. Parameters. R Apply Function To Each Cell In Dataframe. Conditionally Remove Row From Data Frame In R Example Delete Rows Split Apply Combine A Primer For Comtional Biology
Applying Operations Over pandas Dataframes, apply() can apply a function along any axis of the dataframe Apply a square root function to every single cell in the whole data frame. Another usage is to apply a function to each element of a data frame. In the example below I add a dollar sign to each element of the data frame. #Apply function to each element of data frame func = function (x) paste0 ('$', x) x = apply (df, MARGIN = c (1,2), FUN = func) x 1
Apply function to every row in a Pandas DataFrame, It provides with a huge amount of Classes and function which help in analyzing and manipulating data in an easier way. One can use apply()� 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 list, matrix or array and apply a function. Any function can be passed into apply(). In this tutorial, you will learn . apply() function ; lapply( )function
How to Apply Functions on Rows and Columns in R, In R, you can use the apply() function to apply a function over every row or column of a matrix or data frame. This presents some very handy opportunities. The apply () function splits up the matrix in rows. Remember that if you select a single row or column, R will, by default, simplify that to a vector. The apply () function then uses these vectors one by one as an argument to the function you specified. So, the applied function needs to be able to deal with vectors.
- I don't know much about
data.tablebut can you change the
margin =1to iterate over each row?
mt2$meanvar <- setDT(mt2)[,apply(.SD, 1, mean)]- something like this, where mt2 is the
- @Mike thanks, but I need this function to be applied to each cell instead of row or column.
- Thanks! I haven't thought that count 1s would be more convenient in this case.