Delete Every even(uneven) row from a dataset [duplicate] - r

This question already has answers here:
Selecting multiple odd or even columns/rows for dataframe
(5 answers)
Closed 6 years ago.
What is the most elegant way to delete every even/uneven row from a data-frame in R? My first try was to enter the numbers of every second row "by hand" (See below)
(SmallerDataFrame <- OriginalDataFrame[-c(2,4,6,8,10,12),])

Try this
dat <- mtcars$cyl
toDelete <- seq(0, length(dat), 2)
toDelete
dat <- dat[-toDelete, ]
For data frame,
dat <- mtcars
toDelete <- seq(1, nrow(dat), 2)
dat[ toDelete ,]

Related

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)

Duplicate line according to 1 column of a dataframe in R [duplicate]

This question already has answers here:
Repeat each row of data.frame the number of times specified in a column
(10 answers)
Closed 4 years ago.
I have a dataframe like this :
enter image description here
I would like to dupplicate each line the number of times indicated in the column "nombreIndividus".
I tried with rep() and each = and/or time = but I can't do it.
Example :
incomeGlobalCopie <- incomeGlobalCopie[rep(1:nrow(incomeGlobalCopie),
each=incomeGlobalCopie$nombreIndividus)]
Can you help me ?
Thanks
Completely unelegant, but it does the trick:
names <- c("lion","tiger","flamengo")
replication <- c(4,5,3)
species <- data.frame(names, replication)
speciesCopy <- data.frame(matrix(ncol=2,nrow=0))
for(i in 1:length(species$names)){
for(j in 1:species$replication[i]){
speciesCopy <- rbind(speciesCopy, species[i,])
}
}
speciesCopy

R make a list of dataframes by subsetting from a dataframe [duplicate]

This question already has answers here:
Split a large dataframe into a list of data frames based on common value in column
(3 answers)
Closed 4 years ago.
I have a dataframe like the following
x <- c(1:100)
y <- c("a","b","c","d","e","f","g","h","i","j")
y<-rep(y, each=10)
df<-data.frame(x,y)
I would like to make a list of dataframes by subsetting by values in the y column. The end result would produce the same output as something like this:
df1 <- data.frame(df[df$y=="a",])
df2 <- data.frame(df[df$y=="b",])
...
df10 <- data.frame(df[df$y=="j",])
list <- list(df1,df2.....df10)
... but without all of the repetition. Thanks!
split(df, y)
.................

From a dataframe extract columns with numerical values [duplicate]

This question already has answers here:
Selecting only numeric columns from a data frame
(12 answers)
Closed 4 years ago.
I would like to extract all columns for which the values are numeric from a dataframe, for a large dataset.
#generate mixed data
dat <- matrix(rnorm(100), nrow = 20)
df <- data.frame(letters[1 : 20], dat)
I was thinking of something along the lines of:
numdat <- df[,df == "numeric"]
That however leaves me without variables. The following gives an error.
dat <- df[,class == "numeric"]
Error in class == "numeric" :
comparison (1) is possible only for atomic and list types
What should I do instead?
use sapply
numdat <- df[,sapply(df, function(x) {class(x)== "numeric"})]

Splitting data frame in R [duplicate]

This question already has answers here:
How to split a data frame?
(8 answers)
Closed 5 years ago.
I'm new to R. I have a dataset with names in the first row, the category the names belong to in the second row, and then price observations for two year from the third row onwards. I want to split the data frame using the categories in the second row. How do I do this?
This is what my dataset looks like (on R):
This is what I want it look like (on Excel) :
Note: I cannot do this on Excel and then import because there are way too many categories.
Multiple possiblities
df <- data.frame(data = c(1:12), category = rep(letters[1:3], 4))
subset function.
df_a <- subset(df, category == "a")
basic data.frame subset
df_a <- df[df$category == "a",]
into a list
ls <- list
for(category in unique(df$category)){
ls[[category]] <- df[df$category == "a", ]
}
You have the answer in your question. The split or split.data.frame functions would do it. The second argument must be of factor type for this to work.
Example
newdf <- split.data.frame(iris, iris$Species)
newdf

Resources