R: Ordering rows [duplicate] - r

This question already has answers here:
Sort (order) data frame rows by multiple columns
(19 answers)
Closed 6 years ago.
I am trying to order rows by a variable. I have created a sample data frame below and tried to order the rows but the ordering does not appear to work.
# Create vectors for data frame
score <- rep(seq(1:3), 2)
id <- rep(c(2014, 2015), each = 3)
var_if_1 <- rep(c(0.1, 0.8), each = 3)
var_if_2 <- rep(c(0.9, 0.7), each = 3)
var_if_3 <- rep(c(0.6, 0.2), each = 3)
# Generate and print data frame of raw data
foo <- data.frame(score, id, var_if_1, var_if_2, var_if_3)
foo
# Impose arbitrary ordering
bar <- foo[sample(1:nrow(foo)), ]
bar
# Order rows increasing on 'score'
bar[order(score), ]
What am I doing wrong that this doesn't oder the rows on score?

You should use
bar[order(bar$score), ]
Otherwise, you're ordering on the base of the variable "score" instead of the column.

Related

R code that iteratively creates a "rank_order" column for every column in a given dataframe [duplicate]

This question already has answers here:
Get rank for every column using dplyr
(1 answer)
Adding multiple ranking columns in a dataframe in R
(3 answers)
Closed last month.
Given a data frame such as the following, how do I get a rank order (e.g. integer column ranking the value in order from descending as "1,2,3") column output for every single column without writing out ever single column?
df <- data.frame(
col1 = rnorm(100),
col2 = rnorm(100),
col3 = rnorm(100),
col4 = rnorm(100))
rank_col1
rank_col2
rank_col3
etc...
Is this what you want?
df <- cbind(df, as.data.frame(apply(df, 2, rank)))

How to filter a dataframe using a preset vector in R [duplicate]

This question already has answers here:
Select rows from a data frame based on values in a vector
(3 answers)
Test if a vector contains a given element
(8 answers)
Closed last year.
In the dataframe below, I want to filter column code using a preset vector x
# dataframe
set.seed(123)
code <- c(5001,5001,5250,5250,5425,5425,5610,5610,5910,5910)
state <- c("PA","PA","DE","DE","NY","NY","NJ","NJ","CO","CO")
rev_1990 <- runif(10, min=1000000, max=10000000000)
rev_1991 <- runif(10, min=1000000, max=10000000000)
rev_1992 <- runif(10, min=1000000, max=10000000000)
df <- data.frame(code, state, rev_1990,rev_1991,rev_1992)
df
# preset vector
x <- c(5250,5610,5910)
x
My attempt returns only unique rows of x, I want to return all rows containing x
library(dplyr)
df%>%
filter(code == x) # this returns 3 rows instead of 6 rows
Thanks for your attempt
Use %in%:
df %>%
filter(code %in% x)

subset data into dataframe iteratively - R [duplicate]

This question already has answers here:
Split data.frame based on levels of a factor into new data.frames
(3 answers)
Closed 2 years ago.
For this dataframe:
df = data.frame(
col = c("a","f","g","a")
)
How do I subset it for each unique letter and input it into a new dataframe like so?:
sheet_a <- subset(df, col == "a")
sheet_f <- subset(df, col == "f")
sheet_g <- subset(df, col == "g")
I think I need to use a column of unique characters using the below code in a for loop but I'm not sure how
uniq.name_col <- unique(as.vector(df$col))
Thank you for any help!
You can try this, that includes code for exporting dataframes to environment:
#Create list
List <- split(df,df$col)
#Set to envir al dataframes
list2env(List,.GlobalEnv)

Fill numerical values into empty cells of a matrix by dyad ID [duplicate]

This question already has answers here:
R Reshape data frame from long to wide format? [duplicate]
(2 answers)
Complete dataframe with missing combinations of values
(2 answers)
Closed 2 years ago.
I'm working on some bilateral trade data with each row consisting of the ID for exporter and importer, as well as their trade amount. I then want to map the trade amount of each row onto its corresponding cell in a matrix object which has the IDs of "exporter" and "importer" listed as "row" and "column" dimnames.
I am wondering what will be an easier way to do this? Below is my current working code.
# import data
mat <- readRDS(url("https://www.dropbox.com/s/aj1607s975c5gf6/mat.rds?dl=1"))
head(mat, 10)
# import ID
id <- readRDS(url("https://www.dropbox.com/s/6weala2j0idb16i/id.rds?dl=1"))
# create matrix (there are a total of 161 possible IDs though not all of them appear on the data)
matrix <- matrix(rep( 0, len=161*161), nrow = 161)
dimnames(matrix) <- list(unique(id), unique(id))
# how can I fill the trade value (in mat[, 3]) into the corresponding cell on the matrix by match mat[, 1] and mat[, 3] on the dimnames(matrix)?
Try with complete and pivot_wider from tidyr.
library(tidyr)
mat %>%
complete(pid = unique(id), rid = unique(id)) %>%
pivot_wider(names_from = pid, values_from = TradeValue)

Split into list of data frames by column index [duplicate]

This question already has answers here:
How do I split a data frame among columns, say at every nth column?
(1 answer)
What is the algorithm behind R core's `split` function?
(1 answer)
Closed 4 years ago.
Is there an easy way in base R to split a data frame into a list of data frames based on an index factor levels (taken from another data frame)?
For example,
x = data.frame(num1 = 1:26, let = letters, num2 = 10:35, LET = LETTERS)
ls = list(x[, 1:2], x[, 3:4])
But lets say we had an index indicating factor levels for columns, can split be used?
indx = c(1,1,2,2)
? split(x, indx)
It would be the default method of split
out <- split.default(x, indx)
identical(ls, setNames(out, NULL))
#[1] TRUE

Resources