Apply a Function to Each Row in an R Data Frame

In R, you can apply a function to each row in a data frame by using the apply() function with MARGIN = 1. This is useful when you want to calculate a row-wise value, print selected fields from each row, or run the same custom logic for every observation in a data frame.

The important point is that apply() treats each row as a vector. If the data frame has mixed column types, R may coerce the row values to a common type, often character. For purely numeric row-wise calculations, select only the numeric columns before calling apply().

R apply() Syntax for Each Row in a Data Frame

The syntax of R apply() function is

</>
Copy
apply(data_frame, 1, function, arguments_to_function_if_any)

The second argument 1 means that the function is applied row by row. If you use 2, the function is applied column by column.

The general form is:

</>
Copy
apply(X, MARGIN, FUN, ...)

Here, X is the data frame or matrix, MARGIN = 1 means rows, FUN is the function to call for each row, and ... is used to pass extra arguments to the function.

Example 1 – Apply Function for each Row in R DataFrame

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

</>
Copy
# Learn R program to apply a function for each row in r data frame
# R Data Frame
celebrities = data.frame(name = c("Andrew", "Mathew", "Dany", "Philip", "John", "Bing", "Monica"),
				age = c(28, 23, 49, 29, 38, 23, 29),
				income = c(25.2, 10.5, 11, 21.9, 44, 11.5, 45))
# R function
f = function(x, output) {
	# x is the row of type Character
	# access element in first column
	name = x[1]
	# access element in second column
	income = x[3]
	#your code to process x
	cat(name, income, "\n")
}
#apply(X, MARGIN, FUN, …)
apply(celebrities, 1, f)

Output

$ 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

In this example, apply(celebrities, 1, f) calls the function f once for every row in the celebrities data frame. Inside the function, the current row is available as x.

Since the data frame contains both character and numeric columns, the row vector may be treated as character data. That is why this example is suitable for printing or reading values, but for numeric calculations it is better to select numeric columns first.

Apply a Row-wise Calculation to Numeric Columns in R

When the row-wise operation is numeric, pass only the numeric columns to apply(). The following example calculates the total marks for each student.

</>
Copy
marks = data.frame(
  student = c("Asha", "Ravi", "John"),
  maths = c(82, 74, 91),
  science = c(88, 69, 85),
  english = c(79, 81, 87)
)

marks$total = apply(marks[, c("maths", "science", "english")], 1, sum)

print(marks)

Output

  student maths science english total
1    Asha    82      88      79   249
2    Ravi    74      69      81   224
3    John    91      85      87   263

Here, the function sum is applied to each row of the selected marks columns. The result is then stored as a new column named total.

Pass Extra Arguments to a Row Function in R apply()

You can pass extra arguments to the function after the function name in apply(). This is useful when the same row function needs a fixed value such as a bonus, multiplier, threshold, or label.

</>
Copy
scores = data.frame(
  quiz = c(8, 7, 9),
  assignment = c(18, 16, 19),
  exam = c(70, 64, 78)
)

calculate_final_score = function(row, bonus) {
  sum(row) + bonus
}

final_scores = apply(scores, 1, calculate_final_score, bonus = 5)

print(final_scores)

Output

[1] 101  92 111

In this example, bonus = 5 is passed to calculate_final_score() for every row.

Return Multiple Values from Each Data Frame Row

A row function can return more than one value. When every row returns a vector of the same length, you can transpose the result and convert it into a data frame.

</>
Copy
sales = data.frame(
  product = c("A", "B", "C"),
  price = c(100, 150, 200),
  quantity = c(3, 2, 5)
)

row_summary = function(row) {
  revenue = as.numeric(row["price"]) * as.numeric(row["quantity"])
  c(product = row["product"], revenue = revenue)
}

result = as.data.frame(t(apply(sales, 1, row_summary)))

print(result)

Output

  product revenue
1       A     300
2       B     300
3       C    1000

Because this data frame has mixed types, the numeric values are converted with as.numeric() before multiplication. For larger programs, keeping numeric calculations on numeric-only columns usually makes the code easier to read and safer.

Use apply(), lapply(), or for Loop for Row-wise Data Frame Work in R?

Use apply() when you want a compact row-wise operation and each row can be treated as a vector. Use a for loop when the row processing has several steps, side effects, or needs clearer debugging. Use lapply() or related list functions when you are working with lists or want to preserve object structure more carefully.

For row-wise numeric summaries such as totals, averages, minimums, and maximums, R also has direct functions such as rowSums() and rowMeans(). These are often clearer and faster than using apply() for the same task.

</>
Copy
marks$total = rowSums(marks[, c("maths", "science", "english")])
marks$average = rowMeans(marks[, c("maths", "science", "english")])

Common Mistakes When Applying a Function to Each Row in R

  • Using MARGIN = 2 by mistake. Use 1 for rows and 2 for columns.
  • Applying numeric functions to a mixed-type data frame without selecting numeric columns first.
  • Expecting each row to remain a data frame. In apply(), each row is passed as a vector.
  • Forgetting to convert character values back to numeric when a mixed row is used for calculation.
  • Using apply() for simple totals or averages when rowSums() or rowMeans() would be clearer.

FAQs on Applying a Function to Each Row in R DataFrame

How do I apply a function to each row in an R data frame?

Use apply(data_frame, 1, function_name). The value 1 tells R to apply the function row by row.

What does 1 mean in apply(data_frame, 1, FUN)?

In apply(), MARGIN = 1 means rows. MARGIN = 2 means columns.

Why are numeric values treated as character inside my row function?

If the data frame contains mixed column types, apply() may coerce the row to a common type. Select numeric columns first when you need numeric calculations.

Can I create a new column using apply() row by row?

Yes. Store the result of apply() in a new data frame column, such as df$new_column = apply(df[, numeric_columns], 1, FUN).

Should I use apply() or rowSums() for row totals in R?

For row totals, rowSums() is usually clearer. Use apply() when the row-wise function has custom logic that is not already covered by a built-in row function.

Editorial QA Checklist for R Row-wise apply() Tutorial

  • Confirm every row-wise example uses MARGIN = 1 and explains why.
  • Check whether examples with numeric calculations select numeric columns before apply().
  • Verify that output blocks match the R code shown above them.
  • Make sure mixed-type data frame examples mention row coercion clearly.
  • Confirm that simple row totals or averages also mention rowSums() or rowMeans() as alternatives.

Summary: Calling a Function for Each Row in an R Data Frame

In this R Tutorial, we have learnt to call a function for each of the rows in an R Data Frame. Use apply(data_frame, 1, FUN) for row-wise processing, select numeric columns for numeric calculations, and consider rowSums() or rowMeans() when you only need common row summaries.