logo
down
shadow

R: data.table count rows on specific columns > 0


R: data.table count rows on specific columns > 0

By : user2173486
Date : October 20 2020, 08:10 PM
this will help I am try to do a countif function like in excel. I want to countif a select set of columns in a row that are > 0. , Here is a way using rowSums:
code :
dt <- data.table(W1 = sample(0:2, 3, rep = T),
                 W2 = sample(0:2, 3, rep = T),
                 W3 = sample(0:2, 3, rep = T))

dt[, Count := rowSums(.SD > 0), .SDcols = 1:3]

   W1 W2 W3 Count
1:  0  0  0     0
2:  2  1  1     3
3:  0  2  1     2


Share : facebook icon twitter icon
Assign value to specific data.table columns and rows

Assign value to specific data.table columns and rows


By : Christopher Teters
Date : March 29 2020, 07:55 AM
I wish this helpful for you First, it is recommended to use := instead of [<- for efficiency. The [<- is mostly provided for backward consistency. So, I'll first illustrate how to efficiently use := to get what you're after. := is assignment by reference (and it updates a data.table without copying the data, therefore extremely fast).
code :
require(data.table)
DT <- data.table(x = 1:5, y = 6:10, z = 11:15)
DT[2, y := DT[5, y]] 
DT[2, `:=`(y = DT[5, y])]
DT[2, c("y", "z") := as.list(DT[5, c(y, z)])]
DT[2, `:=`(y = DT[5, y], z = DT[5, z])]
DT <- data.table(x = 1:5, y = 6:10, z = 11:15)
DT[1, c("y", "z")] <- as.list(DT[5, c(y, z)])
DT[1, 2:3] <- as.list(DT[5, c(y, z)])
y <- "y"
z <- "z"
# And now try your second case: 
DT[2, c(y, z)] <- as.list(DT[5, c(y, z)])
# the left side takes values from the assignments you made above
# the right side y and z are evaluated within the environment of your data.table
# and so it sees the columns y and z as variables and their values are picked accordingly
y <- "y"
z <- "z"
DT[2, list(y, z)] <- as.list(DT[5, c(y, z)])

# Error in `[<-.data.table`(`*tmp*`, 2, list(y, z), value = list(10L, 15L)) : 
#   j must be atomic vector, see ?is.atomic
DT <- data.table(x = 1:5, y = 6:10, z = 11:15)
tracemem(DT)
# [1] "<0x7fbefb89b580>"

DT[1, c("y", "z") := list(100L, 110L)]
tracemem(DT)
# [1] "<0x7fbefb89b580>"

DT[2, c("y", "z")] <- list(200L, 201L)
# tracemem[0x7fbefacc4fa0 -> 0x7fbefd297838]: # copied, inefficient
Summing across rows of a data.table for specific columns

Summing across rows of a data.table for specific columns


By : Ryan Brigino
Date : March 29 2020, 07:55 AM
around this issue [ Edited 2020-02-15 to reflect current state of data.table ] In recent versions of data.table rowSums(Abundance[ , 4:6]) works as OP originally expected. Here are some alternatives:
code :
Abundance[, SumAbundance := rowSums(.SD), .SDcols = 4:6]
Abundance[, SumAbundance := Reduce(`+`, .SD), .SDcol = 4:6]
C# How to count number of rows in specific column on a data grid table

C# How to count number of rows in specific column on a data grid table


By : Ionut Popovici
Date : March 29 2020, 07:55 AM
With these it helps One solution could be counting non empty cells looping through column values, we could do this using Linq as below.
code :
int count = dataGridView1.Rows
    .Cast<DataGridViewRow>()
    .Select(row => (string)row.Cells["TimeOut"].Value)
    .Where(v => !string.IsNullOrEmpty(v)) 
    .Count();
How to count the the data of specific two columns of different table in PostgreSQL?

How to count the the data of specific two columns of different table in PostgreSQL?


By : Andrew47
Date : March 29 2020, 07:55 AM
may help you . You want to count the number of rows in the union, so translate it into SQL:
code :
SELECT count(*)
FROM (SELECT * FROM table_1
      UNION
      SELECT * FROM table_2) u;
Removing rows in a R data.table with NAs in specific columns

Removing rows in a R data.table with NAs in specific columns


By : Natalie B
Date : March 29 2020, 07:55 AM
Does that help We can select those columns, get a logical vector of NA's based on it using complete.cases and use that to remove the NA elements
code :
data.joined.sample[complete.cases(data.joined.sample[colsofinterest]),]
colsofinterest <- c("lat", "long", "temp", "year", "month", "day", "hour")
data.joined.sample[complete.cases(data.joined.sample[, colsofinterest, with = FALSE])]
Related Posts Related Posts :
  • Authentication failure with rdrop2
  • DT data table display error
  • Issue when adding new rows (with nested dataframes within) to a dataframe
  • R-How to compare two dataframe and update list column value
  • Series vector for approximating pi
  • what is difference between "variance explained " in Random Forest and "merror" in XGBoost
  • R - Cast dataframe on unique rows - reshape2
  • ggplot2: plot correct proportions using geom_bar
  • Speedup query for R data.table - can this two-argument function be applied by group more quickly?
  • apply a function to several columns at once with mutate
  • R 'cowplot' neatly produce gridded plot with shared (common) legends and unique legends
  • Repeat R script for many times and save results to text file
  • How to negative lookbehind for special characters
  • data.table inner join produces error when no match is found
  • Create a new column base on existing column, but row above
  • Is there a way to visualize the process of source() in RStudio?
  • google places api consumes 10 request but I am doing only 1
  • Statistical mode of a categorical variable in R (using mlv)
  • Using for-loop to mutate a data.frame in r
  • Make plot with regression line for mixed model
  • Shortcut to select matces cases in R studio
  • vectoriced norm/matrix multiplication
  • Negative log10 transformation in R
  • Plot data with duplicate points
  • Visualizing crosstab tables with a plot in R - changing colours
  • How to manually modify automated numbers and labels in plot
  • How can I follow any redirections of a url in R?
  • Add jitter to box plot using markers in plotly
  • Adding an extra item to the legend
  • ggplot fills in data in the wrong order
  • Convert list to data frame
  • R: filtering by list(s) of strings and returning all results that start with the content of the lists
  • R:How to attach parts of a data frame with different headers and/or an overflowing piece of the dat frame
  • How to use 'par' for manipulating plot margins?
  • Can dplyr::case_when return mix of NAs and non-NAs?
  • Text preprocessing and topic modelling using text2vec package
  • Uploading multiple files in Shiny, process the files, rbind the results and return a download
  • R levelplot: color green-white-red (white on 0) according to one variable, but show the values of another variable
  • Why [i] doesn't point to the starting point in a vector
  • In R after generating a mvrnorm distribution, Y, what does Y[,1] do?
  • expand a data frame to have as many rows as range of two columns in original row
  • Getting started with R and CFA
  • Re order x-axis in ggplot so time goes from 12AM to 11PM in R
  • R - Automatically stack every nth column of a data frame and save them as new objects
  • How to format dplyr output in R into doubles (or other workable format)?
  • Dataframe to matrix conversion using tapply turns zeros to NAs
  • Smallest multiple of 1:20 - How can I make it quicker?
  • How to specify the size of a graph in ggplot2 independent of axis labels
  • How can I find the number of a vector's elements in another vector?
  • ROC curve from train/test set in caret R package
  • Random Forest for a mixture of categorical,numeric and "unwanted" variables which include missing values
  • extract certain data from multiple excel files with R
  • Matrix with counts of wins and losses between methods in R
  • Grouping string variables from a dataframe by best string match to make subsets
  • Reorder does not work after adding second geom_points
  • cover POS data formate to the one can apply Arules (Apriori)
  • Matching values between data frames based on overlapping dates
  • Grouped bar chart turns into stacked bar chart ggplot
  • R: How to fill in NA Values within a Column based on grouping?
  • Two action buttons, but only the first one, that is written in the server file, works?
  • shadow
    Privacy Policy - Terms - Contact Us © voile276.org