What does 'col.names' do in 'as.data.frame' in R?
If you want to avoid assigning the column names after creating the dataframe, you can utilize the dnn
parameter in the table
function to specify your "name" column, and the responseName
parameter in the as.data.frame
function to specify the "freq" column.
x <- c('a','b','c','a')
x_df <- as.data.frame(table(x, dnn = list("name")), responseName = "freq")
Cannot assign names when converting matrix using as.data.frame
As pointed out in comments, neither data.frame()
nor the matrix
method for as.data.frame
have an argument to let you set column names.
The standard way, as you say, would be to set the names of the object in a second line of code. If that is abhorrent to you, you can still get it done in a single line. Here are two options:
myFrame1 = as.data.frame("colnames<-"(mvrnorm(10, mu = c(0,0), Sigma = matrix(c(1, 0.56, 0.56, 1), ncol = 2), empirical = TRUE), c("x", "y")))
myFrame2 = setNames(as.data.frame(mvrnorm(10, mu = c(0,0), Sigma = matrix(c(1, 0.56, 0.56, 1), ncol = 2), empirical = TRUE)), c("x", "y"))
# I would prefer using two lines, much clearer:
myFrame3 = as.data.frame(mvrnorm(10, mu = c(0,0), Sigma = matrix(c(1, 0.56, 0.56, 1), ncol = 2), empirical = TRUE))
names(myFrame3) = c("x", "y")
# Or, if you're a fine of pipes:
library(magrittr)
myFrame4 = mvrnorm(
10,
mu = c(0,0),
Sigma = matrix(c(1, 0.56, 0.56, 1), ncol = 2),
empirical = TRUE
) %>%
as.data.frame %>%
setNames(c("x", "y"))
When looking at ?as.data.frame
, these are the methods described:
## S3 method for class 'character'
as.data.frame(x, ...,
stringsAsFactors = default.stringsAsFactors())
## S3 method for class 'list'
as.data.frame(x, row.names = NULL, optional = FALSE, ...,
cut.names = FALSE, col.names = names(x), fix.empty.names = TRUE,
stringsAsFactors = default.stringsAsFactors())
## S3 method for class 'matrix'
as.data.frame(x, row.names = NULL, optional = FALSE,
make.names = TRUE, ...,
stringsAsFactors = default.stringsAsFactors())
Notice that the matrix
method does not have a col.names
argument. Only the list
method does. So in converting a list
to a data.frame
, you can use col.names
, but not converting a matrix
.
as.data.frame() no column names (R)
From the as.data.frame
help, it seems that the function does not use the col.names
parameter when applied on a matrix. If you use result
(the original list) instead of do.call(cbind, result)
, you get the column names.
How to set column name in dataframe from function in R
Try creating an empty data frame in the function, and then manually assign the column names you want:
testfunc <- function(columname) {
df <- data.frame(col=character())
names(df) <- columname
return(df)
}
Of course, then you would have an empty data frame, which means you would need to use something like rbind
to add new rows.
The reason your current approach won't work is that you are actually assigning the name columname
to the single column in your data frame. If you want to use a dynamic string name, then you will have to access names()
or something similar.
Pass data frame and reference it for assignment inside function in R
Try not to use <<-
unless absolutely necessary. Functions shouldn't change existing objects. So just have the function return some_data_frame
and then assign it explicitly as needed.
create_blanks<- function(quarters_list,some_data_frame){
for(qcols in c("Q1", "Q2", "Q3", "Q4")){
if(qcols %in% quarters_list){
next
}else{
some_data_frame[,qcols] <- ""
}
} # End of quarter if/else
some_data_frame
} # End of function
data_frame_dummy <- create_blanks(active_quarters, data_frame_dummy)
data_frame_dummy
Q1 Q2 Q3 Q4
1 1 5
2 2 6
3 3 7
4 4 8
Use function argument as name for new data frame in R
You can do it like this...
assign.dataset<-function(dataname){
x<-c(1,2,3)
y<-c(3,4,5)
assign(deparse(substitute(dataname)), rbind(x,y), envir=.GlobalEnv)
}
assign.dataset(new.dataframe.name)
new.dataframe.name
[,1] [,2] [,3]
x 1 2 3
y 3 4 5
R user defined functions: new data frame name as function parameter
I think you can use assign
to do this trick:
subset_high_hp <- function(full_table, df_name) {
sub_df <- full_table %>%
filter(hp > 200)
assign(x = df_name, value = sub_df, envir = globalenv())
}
subset_high_hp(full_table = mtcars, df_name = "mtcars_highhp")
mtcars_highhp
mpg cyl disp hp drat wt qsec vs am gear carb
1 14.3 8 360 245 3.21 3.570 15.84 0 0 3 4
2 10.4 8 472 205 2.93 5.250 17.98 0 0 3 4
3 10.4 8 460 215 3.00 5.424 17.82 0 0 3 4
4 14.7 8 440 230 3.23 5.345 17.42 0 0 3 4
5 13.3 8 350 245 3.73 3.840 15.41 0 0 3 4
6 15.8 8 351 264 4.22 3.170 14.50 0 1 5 4
7 15.0 8 301 335 3.54 3.570 14.60 0 1 5 8
R table() into data.frame changes rownames
Using as.data.frame
with make.names = FALSE
you see why your names are actually changed:
as.data.frame(rbind(
table(orig.df$decade, useNA="always"),
table(orig.df$size, orig.df$decade, useNA="always"),
table(orig.df$plant, orig.df$decade, useNA="always")
), make.names = FALSE)
#> Warning: non-unique values when setting 'row.names':
#> Error in `.rowNamesDF<-`(`*tmp*`, make.names = make.names, value = row.names): duplicate 'row.names' are not allowed
Your row names are duplicated and hence R is trying to fix that. If you want to use a different way of fixing it, you could write a quick function which deals with the rownames on the fly:
table2df <- function(x) {
data.frame(x, row.names = make.unique(paste(rownames(x))))
}
table2df(rbind(
table(orig.df$decade, useNA="always"),
table(orig.df$size, orig.df$decade, useNA="always"),
table(orig.df$plant, orig.df$decade, useNA="always")
))
#> X1910s X1920s X1930s NA.
#> 2 1 1 0
#> high (>5000) 1 0 0 0
#> low (<1) 1 0 1 0
#> medium (11-100) 0 1 0 0
#> NA 0 0 0 0
#> apple 1 0 1 0
#> orange 1 1 0 0
#> NA.1 0 0 0 0
assign names to data frames columns in a list
You can subset the names
of the dataframe:
l <- lapply(l, function(x) {names(x)[1] <-"names";x})
l
In tidyverse
-
library(dplyr)
library(purrr)
l <- map(l, ~.x %>% rename_with(~'names', 1))
From the updated data it seems you have list of matrices and the first column is actually rowname which you can convert to a column and name it.
lapply(results1, function(x) {
mat <- cbind.data.frame(names = rownames(x), x)
rownames(mat) <- NULL
mat
})
#[[1]]
# names x
#1 ..a15.pdf 1.28
#2 ..a17.pdf 1.05
#[[2]]
# names x
#1 ..a15.pdf 2.1
#2 ..a17.pdf 2.2
Related Topics
Making Plot Functions with Ggplot and Aes_String
How to Remove Rows of a Matrix by Row Name, Rather Than Numerical Index
What Are the Differences Between Concatenating Strings with Cat() and Paste()
Find Locations Within Certain Lat/Lon Distance in R
Dplyr::Do() Requires Named Function
Using R to Download Zipped Data File, Extract, and Import .Csv
Shiny - Checkbox in Table in Shiny
Get Margin Line Locations in Log Space
Keeping Only Certain Rows of a Data Frame Based on a Set of Values
Is There a Predict Function for Plm in R
How to Access Global/Outer Scope Variable from R Apply Function
Sum of Two Columns of Data Frame with Na Values
Time Series Plot with X Axis in "Year"-"Month" in R
Set a Functions Environment to That of the Calling Environment (Parent.Frame) from Within Function