logo
down
shadow

Calculate multiple columns from one function and add them to data.frame


Calculate multiple columns from one function and add them to data.frame

By : 戴東華
Date : November 17 2020, 11:52 AM
With these it helps The RHS should be a list of values, and each element of the list gets converted to a column (and recycled if necessary).
Your function already returns a list (of length 1 each) and (Lower+Upper)/2 returns a vector of 3 values (here). In order to return a list, you can use the function c() as follows:
code :
df[, c('Value', 'Error', 'Mid') := c(getIntegral(Lower, Upper), list((Lower+Upper)/2))]
#    Lower Upper     Value        Error Mid
# 1:     1     2 0.5738457 6.370967e-15 1.5
# 2:     2     3 0.5738457 6.370967e-15 2.5
# 3:     3     4 0.5738457 6.370967e-15 3.5


Share : facebook icon twitter icon
calculate mean for multiple columns in data.frame

calculate mean for multiple columns in data.frame


By : user3828549
Date : March 29 2020, 07:55 AM
I think the issue was by ths following , Try colMeans:
But the column must be numeric. You can add a test for it for larger datasets.
code :
colMeans(iris[sapply(iris, is.numeric)])
Sepal.Length  Sepal.Width Petal.Length  Petal.Width 
    5.843333     3.057333     3.758000     1.199333 
microbenchmark(
  plafort = colMeans(big.df[sapply(big.df, is.numeric)]),
  Carlos  = colMeans(Filter(is.numeric, big.df)),
  Cdtable = big.dt[, lapply(.SD, mean)],
  Cdplyr  = big.df %>% summarise_each(funs(mean))
  )
#Unit: milliseconds
#    expr       min        lq     mean    median       uq       max
# plafort  9.862934 10.506778 12.07027 10.699616 11.16404  31.23927
#  Carlos  9.215143  9.557987 11.30063  9.843197 10.21821  65.21379
# Cdtable 57.157250 64.866996 78.72452 67.633433 87.52451 264.60453
#  Cdplyr 62.933293 67.853312 81.77382 71.296555 91.44994 182.36578
m <- matrix(1:1e6, 1000)
m2 <- matrix(rep('a', 1000), ncol=1)
big.df <- as.data.frame(cbind(m2, m), stringsAsFactors=F)
big.df[,-1] <- lapply(big.df[,-1], as.numeric)
big.dt <- as.data.table(big.df)
Calculate a group index across multiple columns of a data frame in R

Calculate a group index across multiple columns of a data frame in R


By : Thandar Phru
Date : March 29 2020, 07:55 AM
To fix this issue What is the most efficient way to calculate a group index (group identifier) across multiple columns in a data frame or data.table in R? , How about this using data.table,
code :
library(data.table)
setDT(df)[,group :=.GRP,by = .(a,b)]
> df
    a b group
 1: 1 a     1
 2: 2 b     2
 3: 1 c     3
 4: 2 a     4
 5: 1 b     5
 6: 2 c     6
 7: 1 a     1
 8: 2 b     2
 9: 1 c     3
10: 2 a     4
11: 1 b     5
12: 2 c     6
Calculate unique combinations between values in multiple columns data.frame R

Calculate unique combinations between values in multiple columns data.frame R


By : MrPerson
Date : March 29 2020, 07:55 AM
this one helps. If I understand correctly, you should be able to do something like this:
code :
ul <- sort(na.omit(unique(unlist(mydf, use.names = FALSE))))
ul
# [1] "blues"   "funk"    "hip hop" "pop"     "punk"    "rap"     "rock" 

tcrossprod(apply(mydf, 1, function(x) table(factor(x, ul))))
#         blues funk hip hop pop punk rap rock
# blues       2    0       1   1    1   2    0
# funk        0    1       0   1    1   0    1
# hip hop     1    0       1   1    0   1    0
# pop         1    1       1   3    1   1    2
# punk        1    1       0   1    2   1    1
# rap         2    0       1   1    1   2    0
# rock        0    1       0   2    1   0    2
mydf <- structure(list(value.1 = c("rock", "pop", "pop", "blues"), value.2 = c("pop", 
    "hip hop", "punk", "punk"), value.3 = c(NA, "rap", "rock", "rap"
    ), value.4 = c(NA, "blues", "funk", NA)), .Names = c("value.1", 
    "value.2", "value.3", "value.4"), row.names = c(NA, 4L), class = "data.frame")
Calculate multiple means of each row from data frame of specific columns

Calculate multiple means of each row from data frame of specific columns


By : tweea
Date : March 29 2020, 07:55 AM
this will help I would like to calculate a mean for each row in this data frame for every four column. , Here is solution with data.table library:
code :
library(data.table)

df <- data.table(df)
df1 <- transpose(df[, -1])[, dd:=substring(colnames(df)[-1],1,9)][,.(Mean1=mean(V1), Mean2=mean(V2), Mean3=mean(V3)),.(dd) ]
output <- transpose(df1)
Use apply family function to create multiple data frame columns by operating on multiple other columns

Use apply family function to create multiple data frame columns by operating on multiple other columns


By : user3279473
Date : March 29 2020, 07:55 AM
I hope this helps you . In case you have hundreds of columns, consider rowSums (similar to apply(mat, margin=1, sum)). And as ?rowSums docs indicate:
code :
df$z1 <- (rowSums(df[grepl("1", names(df))])) /3

df$z2 <- (rowSums(df[grepl("2", names(df))])) /3
df[paste0("z", 1:2)] <- cbind((rowSums(df[grepl("1", names(df))])) / 3,
                              (rowSums(df[grepl("2", names(df))])) / 3)
df[paste0("z", 1:2)] <- sapply(1:2, function(i) rowSums(df[grepl(i, names(df))]) / 3)

df
#   x1 x2 y1 y2        z1       z2
# 1  1  3  1  3 0.6666667 2.000000
# 2  2  4  2  4 1.3333333 2.666667
Related Posts Related Posts :
  • How can I get my points to connect in a plot and show a trend with NA values in data?
  • Read SPecific lines of a CSV file in R-language
  • ggplot stacked bar plot from 2 separate data frames
  • auto.arima not parallelizing
  • Histogram of binned data frame in R
  • R rewriting stringmanipulations implemented in loop to the R-way
  • get first entries in rows of list?
  • Conditionally removing rows from a matrix in R
  • Using a loop to find P(-1.5<Y<1.5) for a range of sample sizes
  • R-Count and list the maximum count row by row
  • Include Iverson Bracket in R documentation
  • update a data frame and environment in R
  • How to write dynamic cumulative multiple in R
  • format time using as.POSIX in R
  • Change the class of multiple columns
  • Remove period and spaces within column headings nested in a list of data frames
  • R: error message --- package error: "functionName" not resolved from current namespace
  • labels with geom_text ggplot2
  • Passing mongodb ISODate in R
  • Importing "csv" file with multiple-character separator to R?
  • Change row names of a table obtained from a lm regression using xtable function
  • R language iterate over R object
  • How do you delete the header in a dataframe?
  • Re coding in R using complicated statement
  • accumulating functions and closures in R
  • How do you combine two columns into a new column in a dataframe made of two or more different csv files?
  • Twitter authentication fails
  • Summing Values of One Vector Conditional on Values of Another Vector
  • draw cube into 3D scatterplot in RGL
  • lme4 translate formula to code in 3-level model
  • How to draw single axis plot in R
  • Combine geom_tile() and facet_grid/facet_wrap and remove space between tiles (ggplot2)
  • Use snpStats with R version 3.0.1
  • Makefile gives strange error while compiling markdown file into .docx file
  • How to determine whether a points lies in an ellipse
  • Summarize data already grouped in r
  • Is the bigvis package for R not available for R version 3.0.1?
  • Operator overloading in R reference classes
  • How to enable user to switch between ggplot2 and gVis graphs in R Shiny?
  • Is there an easy way to separate categorical vs continuous variables into two dataset in R
  • Correct previous year by id within R
  • Installation of rdyncall package for R
  • ggplot2 plot that evaluates the percentage and mean of a third variable at intersecting points
  • Error Handling with Lapply
  • data.table - split multiple columns
  • How to compute the overall mean for several files in R?
  • R: Graph Plotting: Subscripts in the legend like LaTeX
  • Restructuring data in R
  • Distance of pointsfrom cluster centers after K means clustering
  • R incorrect value of date function
  • Package "Imports" not loading in R development package
  • r - run a user defined function several times by taking column elements as parameters
  • Create input$selection to subset data AND radiobuttons to choose plot type in Shiny
  • Generate crosstabulations from dataframe of categorical variables in survey
  • Restructure output of R summary function
  • New behavior in data.table? .N / something with `by` (calculate proportion)
  • search certain number vector in R
  • R version doesn't support quartz graphic device - RStudio won't plot
  • Referencing a function parameter in R
  • How to synchronize signals using a cross-correlation and FFT in R?
  • shadow
    Privacy Policy - Terms - Contact Us © ourworld-yourmove.org