Extract Column from Data.Frame as a Vector

Extract Column from data.frame as a Vector

your.data <- data.frame(Symbol = c("IDEA","PFC","RPL","SOBHA"))
new.variable <- as.vector(your.data$Symbol) # this will create a character vector

VitoshKa suggested to use the following code.

new.variable.v <- your.data$Symbol # this will retain the factor nature of the vector

What you want depends on what you need. If you are using this vector for further analysis or plotting, retaining the factor nature of the vector is a sensible solution.

How these two methods differ:

cat(new.variable.v)
#1 2 3 4

cat(new.variable)
#IDEA PFC RPL SOBHA

Convert data.frame column to a vector?

I'm going to attempt to explain this without making any mistakes, but I'm betting this will attract a clarification or two in the comments.

A data frame is a list. When you subset a data frame using the name of a column and [, what you're getting is a sublist (or a sub data frame). If you want the actual atomic column, you could use [[, or somewhat confusingly (to me) you could do aframe[,2] which returns a vector, not a sublist.

So try running this sequence and maybe things will be clearer:

avector <- as.vector(aframe['a2'])
class(avector)

avector <- aframe[['a2']]
class(avector)

avector <- aframe[,2]
class(avector)

How to extract a pandas dataframe column to a vector

my_col = test['my_column'].tolist()

How to extract single column as vector from data frame

As @nograpes suggested.

as.numeric(rates.data[, 2:9])
## [1] 0.31 0.38 0.50 0.67 0.89 1.34 1.88 2.83

how to extract the column in a dataframe with the values matching to a vector?

We can try

df1[sapply(df1, function(x) all(x %in% cc))]

Or with Filter

Filter(function(x) all(x %in% cc), df1)
# A C
#1 a e
#2 a i
#3 a i
#4 i a
#5 a a

How to extract column_name String and data Vector from a one-column DataFrame in Julia?

f(df) = only(names(df))
g(df) = only(eachcol(df)) # or df[!, 1] if you do not need to check that this is the only column

(only is used to check that the data frame actually has only one column)

An alternate approach to get the column name without creating an intermediate data frame is just writing:

julia> names(df, r"y ")
1-element Vector{String}:
"y (°C)"

to extract out the column name (you need to get the first element of this vector)

Extract column names from df as a vector - or copy / paste column names to new df?

In one line:

df3 <- rbind(df1, setNames(df2, names(df1)))

How could I extract all the numbers from a data frame into a vector?

Extract all the elements with

x = unlist(gt1,use.names=FALSE)

Making them numeric with

x = as.numeric(x)

Now you may remove NAs and duplicates via

x = x[!is.na(x)]
x = unique(x)


Related Topics



Leave a reply



Submit