how do i find the row number of with the largest value in a colume in data.table in r

You can use the .I function in data.table to get the row numbers and which.max function to get the index of the maximum value. Here's an example:

main.r
library(data.table)

# create sample data.table
dt <- data.table(a = c(1, 5, 3), b = c(2, 6, 9), c = c(4, 7, 8))

# find the column with the highest value in row 1
which.max(dt[1,])
# output: "c"

# find the row number with the highest value in column "b"
dt[, .I[which.max(b)]]
# output: 3
291 chars
13 lines

In the above example, we first create a sample data.table and then use the which.max function on the specific row to find the column with the maximum value. We then use .I and which.max function to find the row number with the maximum value in a specific column.

related categories

gistlibby LogSnag