Deleting Rows in R [duplicate] - r

This question already has answers here:
Subset dataframe by multiple logical conditions of rows to remove
(8 answers)
Closed 7 years ago.
I'm trying to delete all rows in a dataframe when the average of a vector > an individual number in the vector. For some reason it seems to pick and choose which ones it deletes. All help is appreciated thank you, here is my code.
k<-c(HW2$AGE)
j<-mean(k)
for (i in HW2$AGE)
if (j>i){
HW2 <- HW2[-i, ]
}

Don't need to vectorise. Instead I would use the below
Sample data
x <- data.frame("A"= runif(10), "B" = runif(10))
Calculate mean
xMean <- mean(x[,"A"])
Exclude rows
y <- x[x$A < xMean,]
This is probably the most obvious way of excluding unwanted rows

Related

Render rows that do not have zero in any columns in R [duplicate]

This question already has answers here:
How to remove rows with 0 values using R
(2 answers)
Closed 2 years ago.
I searched many questions that were suggested by the stackoverflow before posting this question but I couldn't find what I was looking for, I decided to ask here, I have data file:
https://github.com/learnseq/learning/blob/main/GSE133399_Fig2_FPKM.csv
The file has 9 columns, first column has names, the other 8 columns have values, I want to render into an object all columns that do not have zero and save the in csv format.
I had a look on your data set: it contains some rows having all values zero, except the identifier. I assume you want to omit the lines being full of zero's. This code does the job:
data1 = read.csv("GSE133399_Fig2_FPKM.csv")
## Apply <all> on each row.
allZero = apply(data1[, -1] == 0, 1, all)
data2 = data1[!allZero, ]
Now, data2 is the same as data1, but without the rows having only zeros.

How do I subset a dataframe's columns if the data is all the same? [duplicate]

This question already has answers here:
How to remove columns with same value in R
(4 answers)
Closed 2 years ago.
I have a really large dataset and I want to filter out some of the columns because it is the same data all throughout (ex: company name is all "Walmart"). I can go through and do these manually but I'm looking for a code to do it automatically.
I had in mind a function to subset based on if sum(unique(colnam)) == 1 but not sure how to get it to work. Thanks.
which(sapply(dat, function(col) length(unique(col)) == 1))

How can I loop through a determinated range of rows? [duplicate]

This question already has answers here:
Select the row with the maximum value in each group
(19 answers)
Closed 4 years ago.
I am stuck with the beginning of my analysis. Perhaps the question could be stupid, but I would like to request your help for some tips.
I have a dataframe with several variables; and each variable has 10 observations. My doubt is how can I estimate for each variable the max of the first 5 observations, and the max of the following 5 observations.
This is an example of my code:
for (i in 1:length(ncols)){
max.value <- max(var1)
}
Thank you very much in advance
# Load data
data(mtcars)
# Add running ID to check results if you want to
mtcars$ID <- seq.int(nrow(mtcars))
# Make index to separate data 50/50
ind <- seq_len(0.5*nrow(mtcars))
# Make two DFs "first..." and "second..."
firstpart <- mtcars[ind, ]
secondpart <- mtcars[-ind, ]
# Look at the results
View(firstpart)
View(secondpart)
# do your stuff to data

Find total frequency of number In Column [duplicate]

This question already has answers here:
Counting the number of elements with the values of x in a vector
(20 answers)
Closed 5 years ago.
I am new on R. I want to ask, How to find frequency of each Number in Column, there are multiple numbers in column. i want to frequency of each number. I want just simple code. You can imagine that data set name is Oct-TT. Thanks
Here is the answer:
df <- as.data.frame(sample(10:20, 20,replace=T))
colnames(df) <- "Numbers"
View(df)
as.data.frame(table(df$Numbers))

Looping a column in a dataframe while changing its value [duplicate]

This question already has answers here:
Using ifelse in R
(2 answers)
Closed 6 years ago.
I'm quite new to R, picked it up less than two weeks ago and was wondering why this didn't work. Basically what I'm trying to do is to loop through the new added column, compare a value of another column in the same row, and based on a condition, change the value in the column I'm looping.
myDataFrame["column2"] <- "a"
refValue = x
for(i in nrow(myDataFrame){
if(column1[i] >= refValue){
column2[i] <- "b"
}}
Tried to run it but the value doesn't change
View(MyDataFrame)
So myDataFrame is at th moment is
column1---------column 2
someValue------a
someValue------a
someValue------a
someValue------a
after it finished looping based on a condition which is the value of the corresponding row in column1, I want to change some of the 'a's to 'b's
No need to use loop for this. You can replace your code with
myDataFrame$column2 <- with(myDataFrame, ifelse(column1 >= x, "b", "a"))

Resources