Extracting unique values from data frame using R
This is well suited for union
:
data.frame(V1_V2=union(df$v1, df$v2))
# V1_V2
#1 1
#2 2
#3 3
#4 a
#5 b
#6 4
How to extract unique elements from a data.frame in R?
A 'data.frame' can be considered as a 'list' with columns as 'list' elements having the same length. By using unlist
, we can convert it to vector
and then get the unique
values and convert to 'character' class with as.character
.
as.character(unique(unlist(df)))
How to extract unique values from a data frame column?
There are some typos in object names datframe
and dataframe
as well as curly brackets are misplaced:
val_uniques <- function(colname, dataframe) {
if (colname %in% colnames(dataframe)) {
print(unique(dataframe[, colname] , incomparables = FALSE))
} else {
print("cette colonne n'existe pas")
}
}
df <- data.frame(a = c(1, 1, 3, 4), b = 1:4)
val_uniques("a", df)
# [1] 1 3 4
How to extract unique values from a data frame in r
Here is a base R approach:
There are two parts:
- We create a list of positions that occur at least twice using
duplicated
- We look for positions that are not in the list of duplicated positions
Then we subset test
on condition 2.
test[!test$position %in% test$position[duplicated(test$position)],]
# position genomic_regions
#3 chr1_13538 intergenic
How to extract unique rows from a data frame with an index column?
You could use duplicated()
.
> df1[-which(duplicated(df1[,-1])), ]
Index a b c
1 1 12 12 14
3 3 11 12 13
Data
df1 <- structure(list(Index = 1:3, a = c(12L, 12L, 11L), b = c(12L,
12L, 12L), c = c(14L, 14L, 13L)), class = "data.frame", row.names = c(NA,
-3L))
Unique values of a dataframe column to list in R
In base R, you can get unique
values from site_name
and make it a list.
as.list(unique(site_df$site_name))
For example, with default mtcars
this will result into :
as.list(unique(mtcars$cyl))
#[[1]]
#[1] 6
#[[2]]
#[1] 4
#[[3]]
#[1] 8
get unique values of single column in list of data frames in R
We can unlist
the data.frames and get the unique
elements
unique(unlist(dd$data))
#[1] 1 2 3 4
If we need to extract only 'a' column
unique(unlist(lapply(dd$data, `[[`, "a")))
#[1] 1 2 3 4
Or with map/pluck
library(dplyr)
library(purrr)
map(dd$data, pluck, "a") %>%
flatten_dbl %>%
unique
#[1] 1 2 3 4
R dataframe - how to extract unique values
No need to use summarise
here, you can do this (using head
to get the first row)
ddply(sampleData, .(SUBJ, BLK, TR), function(x) head(x,1))
SUBJ BLK TR BEG END
1 1234 1 1 111021 111021
2 1234 2 2 132050 133113
3 5678 1 1 82503 82502
4 5678 2 2 274870 288224
Or more general to get the nth row.n you can do :
ddply(sampleData, .(SUBJ, BLK, TR), function(x) x[min(row.n,nrow(x),])
Related Topics
Fastest Way to Get Min from Every Column in a Matrix
Creating Shiny Reactive Variable That Indicates Which Widget Was Last Modified
How to Use a Non-Ascii Symbol (E.G. £) in an R Package Function
Twitter Sentiment Analysis W R Using German Language Set Sentiws
How to Add Colorbar with Perspective Plot in R
Installing R Packages Error in Readrds(File):Error Reading from Connection
How to Write an Xts Object Using Write.CSV in R
Separate Ordering in Ggplot Facets
Generate 3 Random Number That Sum to 1 in R
R Multiple Conditions in If Statement
How to Force Seasonality from Auto.Arima
Adding Multiple Shadows/Rectangles to Ggplot2 Graph
R Reshape2 'Aggregation Function Missing: Defaulting to Length'
Geom_Smooth and Exponential Fits
Update() Inside a Function Only Searches the Global Environment