searching rows in R until it matches a given value then - r

I ´m getting the first steps in R and perhaps someone could help me. I have a table with n columns and n rows, and what I want to write a script to search each rows for a value, if don´t matches the value than it should proceed to the next row until if matchs the value. Once it matches the value it should go back to the previous row and the concatenate this row with the first column of the table. Can anyone give me any idea on how to make this on R?

Let's you are looking for the first occurrence of value X in the table foo. Try this:
i = min(which(foo==X, arr.ind=T)[,1])
if (i > 1) unlist(c(foo[i-1,], foo[,1]))
You may further remove the names of your result by unname() command or assign your desired names by names().

Related

why is my nested if in for loop not working?

I have a dataframe on bat activity with variables
"plot_no" "date" "rec_hour" "guild" "sunrise" "sunriseEnd" "sunsetStart" "sunset" "count" "id"
where ID is a character combination of plot_no, date, rec_hour and guild.
At the moment, if there were no observations for a certain plot_no, date, time and guild, there is no line in the dataframe. I would like to have all relevant combinations of those variables and have a count of zero when there was no observation.
In order to do that, I made a new dataframe with all combinations and all zeros for count. Now I want to add the right count into the zero dataframe using this code:
for (nr in dim(Bat_Data_zeros_)[1]) {
if (dim(Bat_Data_count[Bat_Data_zeros_$id[nr] == Bat_Data_count$id,])[1]>0) {
Bat_Data_zeros_$count[nr] <- Bat_Data_count$count[Bat_Data_zeros_$id[nr] == Bat_Data_count$id]
}
}
but only one value (not even the first one) gets replaced. I checked every part of the code separately to see if it gave the output I was expecting and it all seemed right. I also tried using str_detect (was my first attempt) on the IDs, but the str_detect function for some reason gives back way more matches than there actually are.

Getting only the rownames containing a specific character - R

I have a Seurat R object. I would like to only select the data corresponding to a specific sample. Therefore, I want to get only the row names that contain a specific character. Example of my differences in row names: CTAAGCTT-1 and CGTAAAT-2. I want to differentiate based on 1 and 2. The code below shows what I already tried. But it just returns the total numbers of row. Not how many rows are matching the character.
length <- length(rownames(seuratObject#meta.data) %in% "1")
OR
length <- length(grepl("-1",rownames(seuratObj#meta.data)))
Idents(seuratObject, cells = 1:length)
Thanks for any input.
Just missing which()
length(which(grepl("-1", rownames(seuratObject#meta.data))))

Conditional sum of rows by a column value

I'm trying to sum rows that contain a value in a different column.
rowSums(wood_plastics[,c(48,52,56,60)], na.rm=TRUE)
The above got me row sums for the columns identified but now I'd like to only sum rows that contain a certain year in a different column. I tried this
rowSums(mydata[,c(48,52,56,60)], na.rm=TRUE, mydata$current_year = '2015')
with no success. I thought I might have to single out the year value from the column number, 7, in the initial column list.
Any help is appreciated.
I would say simply
rowSums(mydata[mydata$current_year == '2015',c(48,52,56,60)], na.rm=TRUE)
since I don't have the original data frame I cannot give you the result. But the idea is that you can select which rows you want before the comma while selecting which column you want. Is this clear enough for you?

how to remove rows from dataframe in r (different situation)

So I want to remove the first row in the data frame. my code is like
reddot.info<-reddot.info[-1,]
But then i find i can not view this data frame. i figure out the reason. because when i run code
reddot.info[1,]
if appears as
Num Product_names URL
1 NA Product Names URL
which means that if i use this code i will also remove the column names.
so what should i do to remove the first row in stead of removing the column names and first row together.
Thank you so much.
You probably don't have column names to begin with because removing the first row like that doesn't remove the column names.
colnames(reddot.info) <- reddot.info[1,]
reddot.info <- reddot.info[-1,]

How to remove duplicated rows by a column in an R matrix

I am trying to remove duplicated rows by one column (e.g the 1st column) in an R matrix. How can I extract the unique set by one column from a matrix? I've used
x_1 <- x[unique(x[,1]),]
While the size is correct, all of the values are NA. So instead, I tried
x_1 <- x[-duplicated(x[,1]),]
But the dimensions were incorrect.
I think you're confused about how subsetting works in R. unique(x[,1]) will return the set of unique values in the first column. If you then try to subset using those values R thinks you're referring to rows of the matrix. So you're likely getting NAs because the values refer to rows that don't exist in the matrix.
Your other attempt runs afoul of the fact that duplicated returns a boolean vector, not a vector of indices. So putting a minus sign in front of it converts it to a vector of 0's and -1's, which again R interprets as trying to refer to rows.
Try replacing the '-' with a '!' in front of duplicated, which is the boolean negation operator. Something like this:
m <- matrix(runif(100),10,10)
m[c(2,5,9),1] <- 1
m[!duplicated(m[,1]),]
As you need the indeces of the unique rows, use duplicated as you tried. The problem was using - instead of !, so try:
x[!duplicated(x[,1]),]

Resources