  C RUBY-ON-RAILS MYSQL ASP.NET DEVELOPMENT RUBY .NET LINUX SQL-SERVER REGEX WINDOWS ALGORITHM ECLIPSE VISUAL-STUDIO STRING SVN PERFORMANCE APACHE-FLEX UNIT-TESTING SECURITY LINQ UNIX MATH EMAIL OOP LANGUAGE-AGNOSTIC VB6 MSBUILD # Calculate multiple columns from one function and add them to data.frame  » r » 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
`````` ## 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

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

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
#  "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

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

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
`````` 