Convert Data Frame into Vector

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)

Using R convert data.frame to simple vector

see ?unlist

Given a list structure x, unlist simplifies it to produce a vector
which contains all the atomic components which occur in x.

unlist(v.row)
[1] 177 165 177 177 177 177 145 132 126 132 132 132 126 120 145 167 167 167
167 165 177 177 177 177

EDIT

You can do it with as.vector also, but you need to provide the correct mode:

 as.vector(v.row,mode='numeric')
[1] 177 165 177 177 177 177 145 132 126 132 132 132 126 120 145 167 167
167 167 165 177 177 177 177

Convert a dataframe to a vector (by rows)

You can try as.vector(t(test)). Please note that, if you want to do it by columns you should use unlist(test).

Convert a row of a data frame to vector

When you extract a single row from a data frame you get a one-row data frame. Convert it to a numeric vector:

as.numeric(df[1,])

As @Roland suggests, unlist(df[1,]) will convert the one-row data frame to a numeric vector without dropping the names. Therefore unname(unlist(df[1,])) is another, slightly more explicit way to get to the same result.

As @Josh comments below, if you have a not-completely-numeric (alphabetic, factor, mixed ...) data frame, you need as.character(df[1,]) instead.

Convert dataframe into named vectors based on column names

You could do something like this (though @MartinGal gives a better method). We can convert each column to a list, then flatten to just have a named vector, then can save to the global environment.

library(tidyverse)

list2env(flatten(apply(df, 2, function(x) as.list(x))), envir = .GlobalEnv)

Convert pandas dataframe to vector

You can use method values on a series.
This returns a numpy array.

import pandas as pd

df = pd.DataFrame({
'Col1': ['Place', 'Country'],
'Col2': ['This', 'That'],
})

vector = df['Col1'].values

print(vector)
print(type(vector))

Output

['Place' 'Country']
<class 'numpy.ndarray'>

Convert data frame into vector

Basically, data frames are lists, so that you can invoke unlist(df).

qqplot would not graph. Error in converting dataframe into a vector

This function is perhaps a little too fancy, but should do what you want. (The qfun.args/do.call nonsense is to allow you to include extra shape parameters for the target distribution, which doesn't seem to be necessary here — because of the way that Q-Q plots are assessed, changes in scale and location parameters don't affect their appearance much.)

It's basically just encapsulating and generalizing the chi-squared example shown in ?qqplot ... to generate the x-variable, you use ppoints() to generate an appropriate set of equally spaced quantile points, then the quantile (q*) function of your target distribution to convert those to theoretical quantiles.

qfun <- function(y, qfun = qnorm, qfun.args = NULL, ...) {
n <- length(y)
qqplot(do.call(qfun,
c(list(ppoints(n)), qfun.args)),
xlab = "",
y, ...)
qqline(y,
distribution = function(p) do.call(qfun, c(list(p), qfun.args)),
probs = c(0.1, 0.6), col = 2)
}

Try it out:

qfun(Qc, main = "Gaussian")
qfun(Qc, qexp, main = "Exponential")

library(VGAM)
qfun(Qc, qgumbel, main = "Gumbel")

Sample Image



Related Topics



Leave a reply



Submit