How can I use the row.names attribute to order the rows of my dataframe in R?
This worked for me:
new_df <- df[ order(row.names(df)), ]
How do I order by row.names in dataframe
I've had this problem before. Here's how I solved it. Using some iris
sample data from base R,
> dat <- iris[sample(nrow(iris), 10), ]
> rn <- rownames(dat)
> dat[order(as.numeric(rn)), ]
How to order a data.frame based on row.names in another data frame?
Here's one option, using the built-in mtcars
data frame for illustration:
# Create new sorted data frame. This is analogous to the second data frame
# by which you want to sort the first one.
mtcars.sorted = mtcars[order(rownames(mtcars)),]
# Sort original data frame by the order of the new data frame
mtcars[match(rownames(mtcars.sorted), rownames(mtcars)),]
So, in your case, the code would be:
DF1[match(rownames(DF2), rownames(DF1)), ]
How can I order the rownames of a dataframe in R?
row_names_test <- data.frame(cbind(genes <- rownames(read.csv("~/row_names_test.csv")),
read.csv("~/row_names_test.csv")))
row_names_test <- row_names_test[order(row_names_test$genes), ]
How to convert row names to column names and bind it by order in r
Assuming your long-form data starts with the rows in the order you want, try this:
month_order = unique(returns)
returns.df <- returns %>%
spread(key = DATE, value = RETURN) %>%
select(c("TICKER", month_order))
If your data doesn't begin in the right order, append a year and convert it to a Date
class object. Then you can sort it to the right order and use the method above.
How to sort each row of a data frame WITHOUT losing the column names
Store the names and apply them:
nm = names(df)
sorted_df <- as.data.frame(t(apply(df, 1, sort)))
names(sorted_df) = nm
You could compress this down to a single line if you prefer:
sorted_df = setNames(as.data.frame(t(apply(df, 1, sort))), names(df))
R, How to set row names attribute as numeric from character?
From R's help on ?row.names
:
All data frames have a row names attribute, a character vector of length the number of rows with no duplicates nor missing values.
This means that the row names will always be a character vector. You would need to use workarounds as suggested in the comments to make them "usable" as integers, basically always coercing. One suggestion could be that you create an id
column of class integer and do not use row.names
as id:
df$id <- as.integer(row.names(df))
df[order(df$id), ]
Omitting row.names
also seems to be the way to go with popular data frame rethinking such as data.table or tibble - none of those use row names.
How to set row names for list of matrix or data frame?
I'm adding @Roland's answer here so the question appears answered.
ismr3 <- lapply(ismr2, function(x){ row.names(x)<-as.character(x$ID); x})
The important part is that the function inside lapply actually returns the value to be added to the list. With just the row.names<-
assignment, the value returned from that is just the row names themselves. So because each assignment was returning a character vector, you were not creating an object that had a dim()
attribute. (Atomic vectors have length
, not dim
).
Related Topics
How to Show Matrix Values on Levelplot
Data.Table Inner/Outer Join with Na in Join Column of Type Double Bug
How to Close Unused Connections After Read_HTML in R
Split Date Data (M/D/Y) into 3 Separate Columns
R Remove Non-Alphanumeric Symbols from a String
Change Text Color for Cells Using Tablegrob
When Using Ggplot in R, How to Remove Margins Surrounding the Plot Area
Marking Specific Tiles in Geom_Tile()/Geom_Raster()
R: 'Split' Preserving Natural Order of Factors
How to Order Bars in Faceted Ggplot2 Bar Chart
Shrink Dt::Datatableoutput Size
R Reshape a Vector into Multiple Columns
R Draw All Axis Labels (Prevent Some from Being Skipped)
Storing Specific Xml Node Values with R's Xmleventparse
The Right Way to Plot Multiple Y Values as Separate Lines with Ggplot2
Loop Character Values in Ggtitle
Aggregate by Factor Levels, Keeping Other Variables in the Resulting Data Frame