retrieve the first and last entry in a row over a set of columns that start with a specific string in r

To retrieve the first and last entry in a row over a set of columns that start with a specific string in R, you can use the dplyr and tidyr packages. Here is a sample code snippet that demonstrates how to achieve this:

main.r
# Load necessary libraries
library(dplyr)
library(tidyr)

# Create a sample dataframe
df <- data.frame(
  id = 1:5,
  col_a1 = c(10, 20, 30, 40, 50),
  col_a2 = c(100, 200, 300, 400, 500),
  col_b1 = c(1000, 2000, 3000, 4000, 5000),
  col_b2 = c(10000, 20000, 30000, 40000, 50000)
)

# Define the prefix
prefix <- "col_a"

# Select columns that start with the prefix
cols <- df %>% 
  select(starts_with(prefix)) %>% 
  names()

# Retrieve the first and last entry in each row
df_result <- df %>% 
  rowwise() %>% 
  mutate(
    first_entry = get(cols[1])[1],
    last_entry = get(cols[length(cols)])[1]
  )

# Print the result
print(df_result)
645 chars
32 lines

In this code:

  • We first create a sample dataframe df.
  • We define the prefix that we want to use to select columns.
  • We use the select function from dplyr to select columns that start with the prefix, and store their names in the cols variable.
  • We use the rowwise function from dplyr to perform row-wise operations.
  • Inside the mutate function, we use the get function to retrieve the first and last columns that start with the prefix, and extract the first entry from them using [1].
  • Finally, we print the resulting dataframe df_result.

This will output the first and last entry for each row across the columns that start with the specified prefix.

related categories

gistlibby LogSnag