Advanced subsetting r data frame [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 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"

Related

Create array with numeric data and characters [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 2 years ago.
Improve this question
I'm new to R. I would like to know how I can create an array with two dimensions, the first being a numeric matrix and the second a character matrix?
Thanks.
An array cant have multiple datatypes. What you are looking for is the dataframe.
For example, you can create a dataframe with a numeric and a character column like this:
df <- data.frame(numericColumn=1:26,characterColumn=LETTERS)
To access these columns, you can use square brackets or the names. For more details, just read the documentation of dataframe or search for some examples.
If you really want to store matrices of different types in one object, you can use a list, for example like
matrixList <- list(numericMatrix=someNumericMatrix,characterMatrix=someCharacterMatrix)

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

R : understanding simplified script with brackets or hooks? [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 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.

get rid of spaces in a vector and then connect in R [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
I have a vector which is the dimnames of another object (let's call it obj) in R:
"I(PT(z))" "trt" "I(PT(z)):trt"
I am not sure how many spaces are there in this output. Now I want to have a resultant vector of "I(PT(z))"+"trt"+"I(PT(z)):trt", i.e., replace the space with "+" signs. The tricky part here is that, length(obj)=3, and obj[[1]] gives "I(PT(z))", and so on. Is there a convenient way to do the concatenation? Thanks.
x <- c("I(PT(z))", "trt", "I(PT(z)):trt")
x
[1] "I(PT(z))" "trt" "I(PT(z)):trt"
paste(x, collapse="+")
[1] "I(PT(z))+trt+I(PT(z)):trt"

Why does data get altered while applying a function [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 loaded a RDS file. The file contains a numeric field. When I say
class(NEI$Emissions)
it returns
"numeric"
The data is in maximum 3 digits and contains 3 digits of decimal. However, when I issue the command
max(NEI$Emissions)
it returns a huge number.
646952
How can I use the numeric values as it is?
R doesn't lie. One of your data points is not what you expect.
Find which row has the problem with this command:
which.max(NEI$Emissions)
then examine that row of your original data. You will find the errant value.

Resources