Apply function for each Row in an R Data Frame - Tutorial Kart

Apply function for each Row in an R Data Frame

R Tutorial ? We shall learn how to apply a function for each Row in an R Data Frame with an example R Script using R apply function.

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.

a p p l y(d a t a _f ra m e ,1,f u n ct i o n ,a rg u m e n t s _t o _f u n ct i o n _i f _a n y)

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.

r_df_for_each_row.R - R Script File

# Learn R program to

apply a function for

each row in r data

frame

# Learn R program to apply a function for each row in r data frame # R Data Frame

celebrities =

d#atRa.Dfraatma eF(rnaammee = c("Andrew", "Mathew", "cDealneyb"r,iti"ePshi=lipd",at"Jao.fhrna"m, e(name = c("Andrew", "Mathew", "Dany", "Philip", "John", "Bing", "Monica"),

"Bing", age

"=Mocn(2ica8ag,"e)2,3=, c4(92,8,

23,

49,

29,

38,

23,

29),

29, 38, 23, 2i9n)c,ome = c(25.2, 10.5, 11, 21.9, 44, 11.5, 45))

income = c(25.2,

10.5, 11, 21.9, 44,

1#1.R5,fu4n5c)t)ion

#f =R function(x, output) { f = #funxcitsiotnh(ex,roowutopfutt)ype Character { # x#isacthcesrsoweleomf etynpt ein first column Charac t er # ancacmeess=exle[m1]ent in first#caoclucmenss element in second column name = x[1] # aincccoemsse e=lexm[e3n] t in seco#nydoucrocluomdne to process x income = x[3] #yocuart(cnoadmeet,oinpcroomces, s"\n") x}cat(name, income, "\n") } #apply(X, MARGIN, FUN, ...)

#aapppplyly(c(Xe,leMbrAitRieGsI,N,1, f) FUN, ...) apply(celebrities, 1, f)

Output

$ Rscript r_df_for_eac h_row.R Andrew 25.2 Mathew 10.5 Dany 11.0 Philip 21.9 John 44.0

$ Rscript r_df_for_each_row.R Andrew 25.2 Mathew 10.5 Dany 11.0 Philip 21.9 John 44.0 Bing 11.5 Monica 45.0 NULL

Conclusion :

In this R Tutorial, we have learnt to call a function for each of the rows in an R Data Frame.

Home - Get Started

R Tutorial R Script File R Working Directory R Data Types R Variables R Operators R Vectors R Matrix

Decision Making

R Decision Making R if R if..else R if..else if...else R switch

Loops

R Loops R repeat loop R while loop R for loop R break

Strings

Strings

R Strings Find length of String in R Extract Substring from a String in R Concatenate two or more Strings in R

Functions

R Functions

DataFrame

R Data Frame Sort R Data Frame by Column For each row in an R Data Frame Import Excel Data into R Dataframe Convert R Dataframe to Matrix R Dataframe - Delete Rows R Dataframe - Drop Columns R Dataframe - Add Column R Dataframe - Change Column Name R Dataframe - Remove Duplicate Rows R Dataframe - Replace NA with 0 Convert Matrix to R Dataframe

Handling Data from Files

R CSV Files - Read, Filter, Write R Read Excel XLS XLSX files

Charts & Graphs

R Pie Charts R Line Graphs

Statistical Analysis

R Mean of a Vector R Median of a Vector

................
................

In order to avoid copyright disputes, this page is only a partial summary.

Google Online Preview   Download