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.
Related
This question already has answers here:
R Create column which holds column name of maximum value for each row
(4 answers)
Closed 1 year ago.
Say we have the following matrix,
x <- matrix(1:9, nrow = 3, dimnames = list(c("X","Y","Z"), c("A","B","C")))
What I'm trying to do is:
1- Find the maximum value of each row. For this part, I'm doing the following,
df <- apply(X=x, MARGIN=1, FUN=max)
2- Then, I want to extract the column names of the maximum values and put them next to the values. Following the reproducible example, it would be "C" for the three rows.
Any assistance would be wonderful.
You can use apply like
maxColumnNames <- apply(x,1,function(row) colnames(x)[which.max(row)])
Since you have a numeric matrix, you can't add the names as an extra column (it would become converted to a character-matrix).
You can choose a data.frame and do
resDf <- cbind(data.frame(x),data.frame(maxColumnNames = maxColumnNames))
resulting in
resDf
A B C maxColumnNames
X 1 4 7 C
Y 2 5 8 C
Z 3 6 9 C
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
This question already has answers here:
R: Count number of objects in list [closed]
(5 answers)
Closed 2 years ago.
I have a dataframe in R, and I am trying to set all cells in the form of a vector, either c(1,2,3) or 1:2 to NA. Is there any easy way to do this?
You can use lengths to count number of elements in each value of column. Set them to NA where the length is greater than 1. Here I am considering dataframe name as df and column name as col_name. Change them according to your data.
df$col_name[lengths(df$col_name) > 1] <- NA
This question already has answers here:
Split data.frame based on levels of a factor into new data.frames
(3 answers)
Closed 4 years ago.
For example, I have a tibble called "data" where the first column is a UserID and the second column is the UserID for a friend of the user from the first column. So for a user with UserId "1" with n friends, there would be n rows with "1" in the first column and the unique UserId's of his friends in the second column. Below is example code for a list of three users with 5 friends each.
data = cbind(c(rep(0, 5), rep(1,5), rep(2, 5)), seq(from=1,to=15, by=1))
I need to convert this tibble into a list of vectors where I can call data_list[["1"]] and return a vector of the UserID's of "1"'s friends. For example from the above code:
`data_list[["1"]]` returns a vector of length 5 `[6 7 8 9 10]`
Anyone know a method of how to do this?
As the OP showed a matrix instead of a data.frame, we can convert it to data.frame and then split it to a list of vectors based on the value of first column 'V1'
data1 <- as.data.frame(data)
data_list <- split(data1$V2, data1$V1)
data_list[["1"]]
#[1] 6 7 8 9 10
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]