Converting two columns of a data frame to a named vector
Use the names
function:
whatyouwant <- as.character(dd$name)
names(whatyouwant) <- dd$crit
as.character
is necessary, because data.frame
and read.table
turn characters into factors with default settings.
If you want a one-liner:
whatyouwant <- setNames(as.character(dd$name), dd$crit)
How to convert two columns of dataframe into named vector?
If you have 2 columns: X and Y in dataframe df1, and you want Y's values to be the names of items with values from X:
myList <- as.list(df1$X)
names(myList) <- df1$Y
For the modified question, the answer is that there is already a functions that does exactly that ( and might have been a better answer that what I gave:
> split(dat$Var2, dat$Var1)
$A
[1] 1 2
$B
[1] 1 3
$C
[1] 3 4 5
Turning a dataframe into a named vector
You can do:
with(df, setNames(Score, Words))
commitment progress implement decision message deficit
-0.9843452 -0.9831530 -0.9785868 -0.9777243 -0.9762919 -0.9752300
Convert two column from dataframe to a vector R
library(dplyr)
data %>%
pull(Cre, name = Cal)
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 multiple data frame columns to numeric vector
You can use setNames
to create a named vector :
vec <- setNames(df$S, df$C)
vec
# F O T
#7.6 8.0 9.0
class(vec)
#[1] "numeric"
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)
Convert dataframe columns to named number vector
Just use setNames
setNames(blah$y, blah$x)
# Red Blood Red Crimson Maroon
# 20 1 14 13
Conveniently extract a named vector from data.frame using dplyr/tidy?
The pull.data.frame
method already accepts an argument for naming. I thought this was available previously, but this might be only in dplyr
1.0, in which case you would need to install from the tidyverse\dplyr
Github repo.
iris %>%
arrange(Sepal.Length) %>%
pull(Sepal.Length, Species)
Related Topics
Missing Legend with Ggplot2 and Geom_Line
Force Character Vector Encoding from "Unknown" to "Utf-8" in R
How to Order the Months Chronologically in Ggplot2 Short of Writing the Months Out
Evaluating Both Column Name and the Target Value Within 'J' Expression Within 'Data.Table'
How to Add a Ggplot2 Subtitle with Different Size and Colour
Date Conversion from Posixct to Date in R
How to Get Name of Variable in R (Substitute)
Error in Plot.New():Figure Margins Too Large, Scatter Plot
Speeding Up the Performance of Write.Table
Arrange Base Plots and Grid.Tables on the Same Page
Logical Operators (And, Or) with Na, True and False
How to Flatten a List of Lists
Ggplot2 Multiple Sub Groups of a Bar Chart
R: Data.Table Cross-Join Not Working
Ggplot2: Facet_Wrap Strip Color Based on Variable in Data Set
How to Plot with a Png as Background
Select Only the First Row When Merging Data Frames with Multiple Matches