Using row-wise column indices in a vector to extract values from data frame
Just use matrix indexing, like this:
dframe[cbind(seq_along(i), i)]
# [1] "g" "b" "f"
The cbind(seq_along(i), i)
part creates a two column matrix of the relevant row and column that you want to extract.
Extract different col in every row of an R data.frame
x <- data.frame(a=c(1,2,3), b=c(3,2,1), c=c(5,6,4))
cols <- c("c", "a", "b")
sapply(1:length(cols),function(i){x[i,cols[i]]})
[1] 5 2 1
Using row-wise column indices in a vector to extract values from data frame
Just use matrix indexing, like this:
dframe[cbind(seq_along(i), i)]
# [1] "g" "b" "f"
The cbind(seq_along(i), i)
part creates a two column matrix of the relevant row and column that you want to extract.
Index values from a matrix using row, col indices
Almost. Needs to be offered to "[" as a two column matrix:
dat$matval <- mat[ cbind(dat$I, dat$J) ] # should do it.
There is a caveat: Although this also works for dataframes, they are first coerced to matrix-class and if any are non-numeric, the entire matrix becomes the "lowest denominator" class.
Column-wise element selection in R
We can use :
df[cbind(vec, 1:ncol(df))]
#[1] 1 4 5
Using cbind
, we create a row and column index to subset values from df
.
cbind(vec, 1:ncol(df))
vec
#[1,] 1 1
#[2,] 2 2
#[3,] 1 3
Using this matrix, we subset values from (row 1, column1), (row2, column2) and row(1, column3).
Extract all column values row-wise by a particular row name in R
We can use ==
to return a logical vector and then it can be used to subset the rows.
M[rownames(M)=='B00813GRG4',, drop=FALSE]
# V1
# B00813GRG4 "A1D87F6ZCVE5NK"
# B00813GRG4 "ABXLMWJIXXAIN"
using the 'B00813GRG4' as row index will return only the first matching element similar to using match
.
M[match('B00813GRG4', rownames(M)),, drop=FALSE]
# V1
#B00813GRG4 "A1D87F6ZCVE5NK"
Selecting data frame value basing on the vectors
apply family in base R
One way to do it is to use sapply
to go through all rows in your query data frame, and use the row
and col
info of each row to index the mtcars
dataset.
EDIT: Credit to @Darren Tsai for providing the mapply
solution.
library(dplyr)
data.frame(row = c(1, 3, 5),
col = c('mpg', 'cyl', 'disp')) %>%
mutate(value = sapply(1:nrow(.), \(x) mtcars[.[x, 1], .[x, 2]]))
# or mapply
mutate(value = mapply(\(x, y) mtcars[x, y], row, col))
rowwise() from dplyr
Another way of doing it is to use rowwise()
:
library(dplyr)
data.frame(row = c(1, 3, 5),
col = c('mpg', 'cyl', 'disp')) %>%
rowwise() %>%
mutate(value = mtcars[row, col]) %>%
ungroup()
Output
row col value
1 1 mpg 21
2 3 cyl 4
3 5 disp 360
Extract columns using int array values as index from a data
Following worked for me diabetes_test[,model,with=FALSE]
. Read https://cran.r-project.org/web/packages/data.table/vignettes/datatable-faq.html and the help page of the data.table package ?data.table
accessing the first value of the vector in dataframe R
You could loop through the vector using sapply
and extracting the first value:
df$Fees <- sapply(df$Fees, "[[", 1)
Related Topics
Directly Creating Dummy Variable Set in a Sparse Matrix in R
How to Arrange an Arbitrary Number of Ggplots Using Grid.Arrange
Printing Newlines with Print() in R
Pretty Ticks for Log Normal Scale Using Ggplot2 (Dynamic Not Manual)
Which Is the Best Method to Apply a Script Repetitively to N .CSV Files in R
Apply a Function Over Groups of Columns
Long Numbers as a Character String
Inserting a Table Under the Legend in a Ggplot2 Histogram
How to See Data from .Rdata File
Insert Blanks into a Vector For, E.G., Minor Tick Labels in R
Replacing Occurrences of a Number in Multiple Columns of Data Frame with Another Value in R
Convert *Some* Column Classes in Data.Table
Transforming a Time-Series into a Data Frame and Back