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 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 columns of data.frame from lists to vectors
Probably, this is what you want :
y[] <- lapply(y, unlist)
This will convert the data into normal data.frame form.
str(y)
#'data.frame': 10 obs. of 9 variables:
# $ member_id : chr "A000055" "A000361" "A000367" "A000369" ...
# $ name : chr "Robert B. Aderholt" "Rodney Alexander" "Justin Amash" "Mark Amodei" ...
# $ party : chr "R" "R" "R" "R" ...
# $ state : chr "AL" "LA" "MI" "NV" ...
# $ district : chr "4" "5" "3" "2" ...
# $ vote_position: chr "No" "Yes" "No" "Yes" ...
# $ dw_nominate : num 0.361 0.331 0.649 0.376 -0.297 0.584 0.387 -0.123 0.277 0.485
#$ bill_num : chr "S47" "S47" "S47" "S47" ...
# $ bill_title : chr "Violence Against Women Reauthorization Act of 2013" "Violence Against Women Reauthorization Act of 2013" "Violence Against Women Reauthorization Act of 2013" "Violence Against Women Reauthorization Act of 2013" ...
If we have list of multiple lengths we can use unnest
and pass a range of columns.
library(tidyr)
library(dplyr)
z <- y %>%
unnest(cols = member_id:district) %>%
type.convert(as.is = TRUE) %>%
arrange(desc(district))
We can use arrange(district)
to sort them in ascending order.
Convert selected dataframe columns to vector representation (including duplicate values)
I would create an the array of strings, because it's only way to support the output format you desire. Then you can simply use .values
to get the column/array from the pandas series
df['output_col'] = (df['product_id'].astype(str) + ':' + df['value'].astype(str)).values
Output:
array(['111:3000', '121:2500', '131:3500', '141:1000', '111:3300',
'151:2000', '161:2300', '171:1300', '181:1500', '191:4500',
'121:6000', '121:1100'], dtype=object)
If you wish to have it as a list instead of an array, simply add .tolist()
after the .values
method. Finally, keep in mind that in Python a ,
is used to separate elements in an array/list instead of ;
Convert data frame into vector
Basically, data frames are lists, so that you can invoke unlist(df)
.
Convert two column from dataframe to a vector R
library(dplyr)
data %>%
pull(Cre, name = Cal)
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 pandas data frame column, which has values of vectors, into tensors
import tensorflow as tf
resume = [[1.0, 2.0], [3.0, 4.0], [5.0, 6.0]]
tf.convert_to_tensor(resume, dtype=tf.float32)
Output
<tf.Tensor: shape=(3, 2), dtype=float32, numpy=
array([[1., 2.],
[3., 4.],
[5., 6.]], dtype=float32)>
Take a look at this link
Related Topics
Adding Text to a Grid.Table Plot
Why Do Some Unicode Characters Display in Matrices, But Not Data Frames in R
Interpolate Na Values in a Data Frame with Na.Approx
Ggplot2: Curly Braces on an Axis
Lib Unspecified & Error in Loadnamespace
Arrange Base Plots and Grid.Tables on the Same Page
Convert Seconds to Days: Hours:Minutes:Seconds
How to Hide or Disable In-Function Printed Message
Concatenate Strings and Expressions in a Plot's Title
When Importing CSV into R How to Generate Column with Name of the CSV
Creating a New Variable from a Lookup Table
The Cause of "Bad Magic Number" Error When Loading a Workspace and How to Avoid It