Home > Mobile >  Getting vectors with row positions of a dataframe column in R
Getting vectors with row positions of a dataframe column in R

Time:09-16

Let's start with a simple example and will explain afterwards my needs. Consider this dataframe:

df <- structure(list(landmark = c("Akanthion", "Asterion R PARIETAL R", 
"Asterion R  TEMPORAL R", "Asterion R OCCIPITAL", "Asterion L PARIETAL L", 
"Asterion L TEMPORAL L", "Asterion L OCCIPITAL", "Basioccipital anterior OCCIPITAL", 
"Basioccipital anterior SPHENOID", "Basion"), MODULE = c(1, 4, 
6, 7, 3, 5, 7, 7, 8, 7)), row.names = c("Akanthion", "Asterion R PARIETAL R", 
"Asterion R  TEMPORAL R", "Asterion R OCCIPITAL", "Asterion L PARIETAL L", 
"Asterion L TEMPORAL L", "Asterion L OCCIPITAL", "Basioccipital anterior OCCIPITAL", 
"Basioccipital anterior SPHENOID", "Basion"), class = "data.frame")

This dataframe is composed by two columns, one of them named as MODULE. This column has 7 different numbers which correspond to different groups (1, 3, 4, 5, 6, 7, 8)

I would like to get a vector for each group in MODULE with the row numbers they are.

For instance:

Group_7 <- c(4, 7:8, 10)
Group_4 <- c(2)

How can I do it automatically? To be honest, this a very small sample of my original dataframe, which is composed by 1487 rows.

CodePudding user response:

We can split on the sequence of rows to a list of vectors with the f from 'MODULE'

lst1 <- split(seq_len(nrow(df)), paste0("Group_", df$MODULE))

-output

lst1
$Group_1
[1] 1

$Group_3
[1] 5

$Group_4
[1] 2

$Group_5
[1] 6

$Group_6
[1] 3

$Group_7
[1]  4  7  8 10

$Group_8
[1] 9

It may be better to keep it in a list rather than creating objects in the global env. if needed, the code is (not recommended)

list2env(lst1, .GlobalEnv)

CodePudding user response:

A tidyverse option that also produces a list of vectors.

library(dplyr)
library(tibble)

df %>%
  mutate(id = row_number()) %>%
  group_by(MODULE) %>%
  mutate(x = list(id)) %>%
  distinct(MODULE, x) %>%
  arrange(MODULE) %>%
  deframe()

# $`1`
# [1] 1
# 
# $`3`
# [1] 5
# 
# $`4`
# [1] 2
# 
# $`5`
# [1] 6
# 
# $`6`
# [1] 3
# 
# $`7`
# [1]  4  7  8 10
# 
# $`8`
# [1] 9 
  • Related