Home > OS >  accessing the first value of the vector in dataframe R
accessing the first value of the vector in dataframe R

Time:05-22

I have a dataframe which consists of various variables and one particular column that has a vector in it. Is there some code to replace the value to the first vector that is found within the column?

ID Fees
1 c(420,700)
2 400
3 c(200,720)

The desired Output:

ID Fees
1 420
2 400
3 200

CodePudding user response:

You could loop through the vector using sapply and extracting the first value:

df$Fees <- sapply(df$Fees, "[[", 1)

CodePudding user response:

Here is another option using map and pluck to pull out the first item in the vectors:

library(tidyverse)

df %>%
  mutate(Fees =  map(Fees, pluck, 1))

Output

  ID Fees
1  1  420
2  2  400
3  3  200

Or another option using rowwise and an index:

df %>% 
  rowwise %>% 
  mutate(Fees = Fees[1])

We could also use pluck with rowwise:

df %>%
  rowwise %>%
  mutate(Fees = pluck(Fees, 1))

Or we could just skip the use of purrr and use first with rowwise:

df %>%
  rowwise %>%
  mutate(Fees = first(Fees))

Data

df <- structure(list(ID = 1:3, Fees = list(c(420, 700), 400, c(200, 
720))), row.names = c(NA, -3L), class = "data.frame")

CodePudding user response:

You can use map_dbl() from purrr to extract the n-th element of each element in a list.

library(tidyverse)

df %>%
  mutate(Fee1 = map_dbl(Fees, 1))

# # A tibble: 3 × 3
#      ID Fees       Fee1
#   <int> <list>    <dbl>
# 1     1 <dbl [2]>   420
# 2     2 <dbl [1]>   400
# 3     3 <dbl [2]>   200

Data
df <- structure(list(ID = 1:3, Fees = list(c(420, 700), 400, c(200, 720))),
row.names = c(NA, -3L), class = "data.frame")

CodePudding user response:

We could do it this way:

The bottle neck is that Fees is list. With paste we make it character. Then we use parse_number. This extracts always only the first number:

library(readr)
library(dplyr)

df %>% 
  mutate(Fees = parse_number(paste(Fees)))
  ID Fees
1  1  420
2  2  400
3  3  200
  • Related