R : understanding simplified script with brackets or hooks? [closed] - r

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
I would like to understand how really works this script :
y <- y[keep, , keep.lib.sizes=FALSE]
in :
keep <- rowSums(cpm(y)>1) >= 3
y <- y[keep, , keep.lib.sizes=FALSE]
I do know d.f[a,b] but I can not find R-doc for d.f[a, ,b].
I tried "brackets", "hooks", "commas"... :-(
(Sometimes I would prefer that one does not simplifie his R script !)
Thanks in advance.

Subscripting data.Frames takes two values: df[rows, columns]. Any third value are optional arguments that you can use to subscript.
The most common of those is drop=FALSE as in df[1:18, 3, drop = FALSE]. This is done because when you subset just one column of a data.frame, it will lose the data.frame class. In your specific case, it seems like you are using another object that looks like a data.frame but with added functionalities from the bioconductor package. A look at the methods for those will tell you how these work.

Related

Does R has method like fillna(method = "ffill") in python? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 1 year ago.
Improve this question
I am learning using R to do data cleaning work. Just encounter a question that I could deal with by python but not in R.
The dataset is like this.dataset
I want to concat the first two columns and assign it as index. The first thing I need to do is to fillna('ffill') the first column. Then I need concat two columns.
Could you tell me how to do this in R (tidyverse is better)?
The result should like this:
result
Thanks in advance!
Try these. Be sure to read the help pages since many of them have arguments which may need to be set depending on what you want.
zoo::na.locf (last observation carried forward)
zoo::na.locf0
tidyr::fill
data.table::nafill
zoo also has na.aggregate, na.approx, na.contiguous, na.fill, na.spline, na.StructTS and na.trim for other forms of NA filling and tidyr also has replace_na.

Passing a matrix as an parameter to a function in R programming language [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
Is it possible to pass an entire (square) matrix as the argument of a function in R ?
I mean, what I want is that while running the programme, an user will create his / her desired matrix (Say A) in the R console, and if the name of the function is cdecom (Created by me) , the user will write cdecom(A) to get the required output. A possible demonstration of the input by an user is as follows :
A <- matrix( 1:25 , nrow=5 , byrow=TRUE)
cdecom(A)
Is this somehow possible ? Thanks in advance. (If my question is unclear, please see the 3rd comment)
Maybe you can use class(A)=="matrix" and stopifnot within your function cdecom like below
cdecom <- function(A) {
stopifnot(class(A)=="matrix")
...
}

How come I am able to pass a dataframe into 'dparams' argument in 'geom_qq' function, when it is specified as a 'list' type argument? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
How come I am able to pass a dataframe into the dparams argument of the geom_qq() function, when it is specified as a list type argument?
Is it because a dataframe is technically a list of equal length vectors?
I would say so. A data frame can in general be treated like a list, not necessarily the other way around though.
Without knowing your data, your general idea is correct.
See also:
http://www.r-tutor.com/r-introduction/data-frame

Advanced subsetting r data frame [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
Without using a special function, can you do the following to change the values in an R dataframe from a column that have length greater than 4:
df[length(df$Column1)>4,"Column1"] = "replacement value"
This does not seem to work, is there an alternative index style I can use, or do I need to use a function?
Thanks
The function to determine the length of an entry, like a word in a dataframe, is nchar(), and not length(). The latter is typically used to determine the number of entries in a vector.
You could therefore try using:
df[nchar(df$Column1) > 4, "Column1"] <- "replacement value"

Simple R function with variable number of arguments [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I am working on a project converting a bunch of stata code to R to perform data cleaning, and one of the things I'm trying to do is to write a single R function that cleans all of my Yes/No variables that were previously coded as (Yes = 1, No = 2) to standard dummy variables.
The thing is that the number of variables that need to be cleaned by this function will constantly be changing. So my guess is that the function will need to take as its arguments (1) the dataset/dataframe with all the variables, and (2) the list of variables that need to be cleaned.
Any help on this would be greatly appreciated, as I'm pretty new to R.
Thanks!
You could try this:
example <- data.frame(sex=runif(10),q1=rep.int(c(1,2),5),q2=rep.int(c(2,1),5))
yesno <- function(data, variables) {
data.new <- data
data.new[,names(data) %in% variables] <- -data[,names(data) %in% variables]+2
return(data.new)
}
example
yesno(example, c("q1","q2"))
sapply(data, function(x) {-x+2})
data contains your columns of 1, 2. The anonymous functions turns all Yes/1 into 1, and No/2 into 0.

Resources