Convert which() to logical vector [duplicate] - r

This question already has answers here:
How to create a binary vector with 1 if elements are part of the same vector?
(4 answers)
Closed 7 years ago.
I want to create a vector of 0's and 1's such that all elements in y that are 4 become 1 and all other values become 0. which(y == 4) returns the indices in y that are 4, and then y[which(y == 1)] returns a vector of all 4's. How would I produce a vector of 1's where they were 4's and 0's otherwise?
x <- y[which(y == 4)]

Use logical coercion:
as.integer(y==4)
The shorter version:
+(y==4)

Related

How to add cells based off of a specific integer? [duplicate]

This question already has answers here:
Sum elements of a vector beween zeros in R
(3 answers)
Closed 2 years ago.
I want to add values from a column. They go in sequence:
0,225,2352,34234,23442,23456,0,123,...
I want to add the values from 0 until the following 0 but not including the second.
For example, i want an output of
(0+225+2352+34234+23442+23456),(0+123+,...,),...
I want to store them as a new column of totals
One simple solution in base R is
sapply(split(x, cumsum(x == 0)), sum)
With split you basically create groups of elements that you want to sum together using sapply. The final result will be a named numeric vector.
Sample data
x <- c(0,225,2352,34234,23442,23456,0,123,2,0,1,42)
sapply(split(x, cumsum(x == 0)), sum)
# 1 2 3
# 83709 125 43

How to extract max value from vector and position it occurs in R? [duplicate]

This question already has answers here:
Get all the maximum value indexes in a R vector
(2 answers)
Closed 2 years ago.
In a vector in R, how do you obtain the position where the maximum value occurs?
V[i] <- c(1,2,3,4,5,6,7)
max(V)
# [1] 7
How do I get the position that the max value occurs in the vector? In this case, the output would be 7 because V[7]=7
you can do it like this
V <- c(1,2,3,7,5,6,4)
needle <- max(V)
position <- match(needle, V)
V[position] # returns 7

Countif function in R [duplicate]

This question already has answers here:
Sum rows in data.frame or matrix
(7 answers)
Count number of columns by a condition (>) for each row
(4 answers)
Closed 6 years ago.
I have a matrix of n*m dimension. I wanted to count the number of columns in a row which has a value greater than "X". How to do this in R? Please let me know.
You can try rowSums
X <- 0.5
rowSums(m1 > X)
explanation
m1 > X will create a TRUE/FALSE logical matrix. Since TRUE values are treated as 1 and FALSE values are treated as 0, rowSums(m1 > X) will give you a count for each row of the number of values in that row that is greater than X.
data
set.seed(24)
m1 <- matrix(rnorm(5*10), ncol=5)

subsetting 1-column matrix deletes rownames [duplicate]

This question already has an answer here:
How to subset matrix to one column, maintain matrix data type, maintain row/column names?
(1 answer)
Closed 8 years ago.
When I try to subset a 1-colum matrix by it's row names the subsetting works but an numeric vector is returned.
can you somehow prevent that behaviour and keep the row names?
M<-as.matrix(rnorm(5))
rownames(M)<-LETTERS[1:5]
M
[,1]
A 0.6250957
B 0.7330598
C -0.7127075
D 0.2162602
E 0.2223444
M <- M[which(rownames(M) != "A")]
M
## [1] 0.7330598 -0.7127075 0.2162602 0.2223444
you can read about argument drop in the help page: ?'['
M[which(rownames(M) != "A"), ,drop=FALSE]

R, accessing a column vector of a matrix by name [duplicate]

This question already has answers here:
Extract matrix column values by matrix column name
(2 answers)
Closed 7 years ago.
In R I can access the data in a column vector of a column matrix by the following:
mat2[,1]
Each column of mat2 has a name. How can I retrieve the data from the first column by using the name attribute instead of [,1]?
For example suppose my first column had the name "saturn". I want something like
mat2[,1] == mat2[saturn]
The following should do it:
mat2[,'saturn']
For example:
> x <- matrix(1:21, nrow=7, ncol=3)
> colnames(x) <- paste('name', 1:3)
> x[,'name 1']
[1] 1 2 3 4 5 6 7
Bonus information (adding to the first answer)
x[,c('name 1','name 2')]
would return two columns just as if you had done
x[,1:2]
And finally, the same operations can be used to subset rows
x[1:2,]
And if rows were named...
x[c('row 1','row 2'),]
Note the position of the comma within the brackets and with respect to the indices.

Resources