Convert row names into first column
Or you can use tibble
's rownames_to_column
which does the same thing as David's answer:
library(tibble)
df <- tibble::rownames_to_column(df, "VALUE")
Note: The earlier function called add_rownames()
has been deprecated and is being replaced by tibble::rownames_to_column()
assign rownames within a loop to constructed variables names
Based on Roland's comment, I come out with this solution:
rows<-c("a","b")
df<-data.frame(var1=c(1,2),var2=c(10,20))
dfs<-list()
for (n in 1:2){
dfs[[n]]<-df
rownames(dfs[[n]])<-rows
}
A list is the key!
Convert the values in a column into row names in an existing data frame
This should do:
samp2 <- samp[,-1]
rownames(samp2) <- samp[,1]
So in short, no there is no alternative to reassigning.
Edit: Correcting myself, one can also do it in place: assign rowname attributes, then remove column:
R> df<-data.frame(a=letters[1:10], b=1:10, c=LETTERS[1:10])
R> rownames(df) <- df[,1]
R> df[,1] <- NULL
R> df
b c
a 1 A
b 2 B
c 3 C
d 4 D
e 5 E
f 6 F
g 7 G
h 8 H
i 9 I
j 10 J
R>
Convert column/variable to row names for multiple lists in R with sapply
Because in R functions return the last line or explicit return()
call, your anonymous function function(x) { rownames(x) <- x$zipcode }
returns the result of row.names
which per docs:
row.names
returns a character vector.
This can be quickly fixed by calling x after row names change as commented, function(x) { rownames(x) <- x$zipcode; x }
or function(x) { rownames(x) <- x$zipcode; return(x) }
.
However, consider row.names<-
(subtle difference with assignment operator embedded):
function(x) { `rownames<-`(x, value= x$zipcode }
which per same docs:
row.names<-
returns a data frame with the row names changed.
Altogether, removing redundant curly braces {...}
and default USE.NAMES = TRUE
:
test_list <- sapply(polygon_nyc_listings,
function(df) `row.names<-`(df, value = df$zipcode),
simplify = FALSE)
How to make column 0: rownames a number starting at 1 like a regular dataset in r?
We can use rownames_to_column
from tibble
library(tibble)
df1 <- rownames_to_column(df1, "Variable")
Or using base R
df1$Variable <- row.names(df1)
row.names(df1) <- NULL
set row names of dataset to be equal to another variable
very simple.
row.names(yourdf) <- yourdf$psid
How to convert a column to row.names from a tibble
First, you can see the difference
> str(entrepreneur[, 1])
tibble [5 x 1] (S3: tbl_df/tbl/data.frame)
$ Factors: chr [1:5] "Competition" "Cultural Support" "Financing" "High Growth" ...
> str(entrepreneur[[1]])
chr [1:5] "Competition" "Cultural Support" "Financing" "High Growth" ...
Try the code below (using entrepreneur[[1]]
rather than entrepreneur[,1]
)
> `rownames<-`(as.data.frame(entrepreneur[-1]), entrepreneur[[1]])
Baden-Wⁿrttemberg Bayern Berlin Brandenburg
Competition 0.71 0.67 1.00 1.00
Cultural Support 0.66 0.66 0.56 0.55
Financing 0.81 0.83 0.90 0.64
High Growth 0.62 0.77 0.82 1.00
Human Capital 0.46 0.49 0.79 0.77
How do I create a variable based on selected characters from row.names?
Specifically, keep all text prior to the first .
character:
df$Name <- sub('[.].*','', rownames(df))
In a regular expression, .
means to match any character. So to match a literal dot, the character must be escaped. However, within a character class (bounded by [
and ]
), the .
does represent a literal dot. That is, [.]
matches a literal dot only.
Following that is .*
which matches any number of any characters.
Thus, the expression matches everything from the first literal .
to the end of the string, and replaces it with the empty string.
Related Topics
R Output Without [1], How to Nicely Format
Removing Traces by Name Using Plotlyproxy (Or Accessing Output Schema in Reactive Context)
How to Include Custom CSS in HTMLwidgets for R And/Or Leafletr
Drawing a Stratified Sample in R
Drawing Non-Intersecting Circles
Randomly Sample Data Frame into 3 Groups in R
How to Neatly Align the Regression Equation and R2 and P Value
Quickest Way to Read a Subset of Rows of a CSV
R Plotly How to Get 3D Surface with Lat, Long and Z
How to Access Browser Session/Cookies from Within Shiny App
Calculate Summary Statistics (E.G. Mean) on All Numeric Columns Using Data.Table
R Histogram with Multiple Populations
How to Use Variables Newly Created in 'J' in the Same 'J' Argument
Merge Records Over Time Interval
Read.Table Reads "T" as True and "F" as False, How to Avoid
How to Convert a Character String Date to Date Class If Day Value Is Missing
Difference Between 'Paste', 'Str_C', 'Str_Join', 'Stri_Join', 'Stri_C', 'Stri_Paste'