Convert Named Character Vector to data.frame
It's as simple as data.frame(as.list(testVect))
. Or if you want sensible data types for your columns, data.frame(lapply(testVect, type.convert), stringsAsFactors=FALSE)
.
How do I change a named vector to a data frame retaining the names?
As said by @Axeman in the comments, you have a named vector. The easiest way to transform that into a dataframe is by using the stack
-function from base R.
Some example data:
output <- setNames(c(92.08,90.68,54.09,92.87,97.40), LETTERS[1:5])
which looks like:
> output
A B C D E
92.08 90.68 54.09 92.87 97.40
Transform into a dataframe with:
df <- stack(output)
which gives:
> df
values ind
1 92.08 A
2 90.68 B
3 54.09 C
4 92.87 D
5 97.40 E
To get the columnnames and column order as specified in the question:
df <- setNames(stack(output)[2:1], c('Category','Value'))
which gives:
Category Value
1 A 92.08
2 B 90.68
3 C 54.09
4 D 92.87
5 E 97.40
converting a vector into a dataframe columnwise
You can transpose the vector and convert it into dataframe/tibble.
t(x) %>% as_tibble()
t(x) %>% data.frame()
# estimate ci.low ci.up
#1 0.595 0.11 2.004
create a data frame from named vector in R
We can use
vc <- c(1,2,5,6,7,9,12,15,18,19,27)
quartiles <- quantile(vc, probs = seq(0,1,0.01))
df <- data.frame(names(quartiles), quartiles, row.names = NULL)
colnames(df) <- c("quartile", "pvalue")
df$quartile <- as.numeric(gsub("%","", df$quartile))
> df
quartile pvalue
1 0 1.0
2 1 1.1
3 2 1.2
4 3 1.3
5 4 1.4
6 5 1.5
Convert named vector to data frame using attribute values
You can use indexing by row/column value to do this efficiently:
row.nums <- as.numeric(sapply(strsplit(names(a), "\\."), "[", 1))
col.nums <- as.numeric(sapply(strsplit(names(a), "\\."), "[", 2))
mat <- matrix(NA, max(row.nums), max(col.nums))
mat[cbind(row.nums, col.nums)] <- a
mat
# [,1] [,2]
# [1,] "foo" "bar"
# [2,] "dog" "cat"
Convert named vector to df with bind_rows
We can also use stack
to convert named vector to dataframe and then use rename_all
stack(x)[2:1] %>% dplyr::rename_all(~c("Feature","Gain"))
# Feature Gain
#1 spend_7d 0.704766041
#2 d7_utility_sum 0.168417670
#3 recent_utility_ratio 0.101030461
#4 IOS 0.009606189
#5 is_publisher_organic 0.008247149
#6 is_publisher_facebook 0.007932490
data
x <- c(spend_7d = 0.704766041126329, d7_utility_sum = 0.168417670114147,
recent_utility_ratio = 0.101030461314979, IOS = 0.0096061893294683,
is_publisher_organic = 0.008247148545793, is_publisher_facebook = 0.0079324895692841)
Convert character vector to data.frame after str_extract()
Maybe you can use gsub
to remove double quotes from the string and "\n"
.
objeto <- gsub('"|\n|,', '', objeto)
which returns :
objeto
#[1] "CONTRATAÇÃO DE EMPRESA PARA A PRESTAÇÃO DE SERVIÇOS ESPECIALIZADOS DE AFERIÇÃO DO DESEMPENHO E DA QUALIDADE DA CONCESSIONÁRIA MINAS ARENA GESTÃO DE INSTALAÇÕES ESPORTIVAS S"
This can also be written as :
objeto <- gsub('["\n,]', '', objeto)
Or using str_remove_all
from stringr
library
stringr::str_remove_all(objeto, '[\n",]')
which is a shortcut for str_replace_all
stringr::str_replace_all(objeto, '[\n",]', '')
Rename a named vector when name has a match in dataframe
Simple
names(list1)=df$new_name[match(names(list1),df$ID)]
3 49 163 223 360 429
0.17492311 0.11435087 0.07615158 0.10201893 0.14140281 0.13680918
Related Topics
How to Add a Cumulative Column to an R Dataframe Using Dplyr
How to Make Geom_Text Plot Within the Canvas's Bounds
Building R Package and Error "Ld: Cannot Find -Lgfortran"
Get Values and Positions to Label a Ggplot Histogram
Select Every Other Element from a Vector
Split Up a Dataframe by Number of Rows
Replace Negative Values by Zero
Split Up '...' Arguments and Distribute to Multiple Functions
Linear Regression Loop for Each Independent Variable Individually Against Dependent
Remove/Collapse Consecutive Duplicate Values in Sequence
Avoid Clipping of Points Along Axis in Ggplot
Logical Operators (And, Or) with Na, True and False
Last Observation Carried Forward in a Data Frame
How to Get the Maximum Value by Group