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.r645 chars32 lines
In this code:
df
.select
function from dplyr
to select columns that start with the prefix, and store their names in the cols
variable.rowwise
function from dplyr
to perform row-wise operations.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]
.df_result
.This will output the first and last entry for each row across the columns that start with the specified prefix.
gistlibby LogSnag