Help with vectors

Hi All,

I would like to save the values od a column as vectors in the following format:

code_cds = c("CA101","CA102","US103","US104")

I have a huge list of codes that I want to apply and my current function takes any values in the format c("value1", "value2", ...so on)

If I convert as follows, then it throws an error as it gets saved as list. If I use the column as is, then also it throws an error.
How can I save the values in format like c("value1", "value2", ...so on) which is the output format.

Also, how can we append another set of vectors in this set.
Examples: cds = c("CA105","CA106","US102","US101") to be added in code_cds which would then be code_cds = c("CA101","CA102","US103","US104", "CA105","CA106","US102","US101")

library(tidyverse)
library(lubridate)
Codes <- data.frame(
  
  Codes = c("CA101","CA102","US103","US104")

)

code_cds <- Codes %>%
  select(Codes) %>%
  as.vector()

Thanks!

You can get a vector from either a data frame or a tibble if you subset using the [[ ]]syntax.

library(tibble)
library(dplyr)
#> 
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#> 
#>     filter, lag
#> The following objects are masked from 'package:base':
#> 
#>     intersect, setdiff, setequal, union

Codes <- data.frame(
  
  Codes = c("CA101","CA102","US103","US104")
  
)
class(Codes)
#> [1] "data.frame"

#original code
code_cds <- Codes %>%
  select(Codes) %>%
  as.vector()
class(code_cds)
#> [1] "list"

#Use [[ ]] with the data frame
code_cds <- Codes[["Codes"]] 
class(code_cds)
#> [1] "character"

#Make a tibble
Codes <- tibble(Codes = c("CA101","CA102","US103","US104"))  
class(Codes)
#> [1] "tbl_df"     "tbl"        "data.frame"

#Use [[ ]] with the tibble
code_cds <- Codes[["Codes"]] #%>%
class(code_cds)
#> [1] "character"

Created on 2023-09-29 with reprex v2.0.2

Or just

Codes <- data.frame(
  
  Codes = c("CA101","CA102","US103","US104")
  
)

(codes <- unlist(Codes))
#>  Codes1  Codes2  Codes3  Codes4 
#> "CA101" "CA102" "US103" "US104"
is.vector(codes)
#> [1] TRUE

# remove names if desired
attributes(codes) <- NULL
codes
#> [1] "CA101" "CA102" "US103" "US104"

Created on 2023-09-29 with reprex v2.0.2

if the subset brackets become confusing. Works the same way with tibbles.

Thanks @technocrat and @FJCC !

I am looking for output to be looking like in this format: c("CA101","CA102","US103","US104").
I have selected unique values from a column and I would like to store those values in this format which can be used anywhere in the code

That’s not a format, but an expression. The examples given by @FJCC and myself produce vectors that are identical to what you’d get entering the quoted line.

or just:

code_cds <- Codes %>%
  pull(Codes)

is.vector(code_cds)
[1] TRUE

Can be used in cases you have more columns, pull just extracts the given column as a vector.

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.