sapply

R: How to sum pairs in a Matrix by row?

跟風遠走 提交于 2019-12-02 00:01:36
Probably this would be easy. I have a Matrix: testM <- matrix(1:40, ncol = 4, byrow = FALSE) testM [,1] [,2] [,3] [,4] [1,] 1 11 21 31 [2,] 2 12 22 32 [3,] 3 13 23 33 [4,] 4 14 24 34 [5,] 5 15 25 35 [6,] 6 16 26 36 [7,] 7 17 27 37 [8,] 8 18 28 38 [9,] 9 19 29 39 [10,] 10 20 30 40 and I want to "reduce" the matrix summing column pairs by row. Expected result: [,1] [,2] [1,] 12 52 [2,] 14 54 [3,] 16 56 [4,] 18 58 [5,] 20 60 [6,] 22 62 [7,] 24 64 [8,] 26 66 [9,] 28 68 [10,] 30 70 I tried this but doesn't work X <- apply(1:(ncol(testM)/2), 1, function(x) sum(testM[x], testM[x+1]) ) Error in apply

How to void type conversion in R's apply (bit64 example)

半腔热情 提交于 2019-12-01 21:59:21
问题 I am using the bit64 package in some R code. I have created a vector of 64 bit integers and then tried to use sapply to iterate over these integers in a vector. Here is an example: v = c(as.integer64(1), as.integer64(2), as.integer64(3)) sapply(v, function(x){is.integer64(x)}) sapply(v, function(x){print(x)}) Both the is.integer64(x) and print(x) give the incorrect (or at least) unexpected answers (FALSE and incorrect float values). I can circumvent this by directly indexing the vector c but

Sum pairs of columns by group

﹥>﹥吖頭↗ 提交于 2019-12-01 21:57:23
问题 I wish to sum pairs of columns by group. In the example below I wish to sum pairs ( v1 and v2 ), ( v3 and v4 ), and ( v5 and v6 ), each by r1 , r2 and r3 . I can do this using the sapply statement below and I get the correct answer. However, the required code is complex. Could someone show me how to do the same operation perhaps in package data.table or with rollapply and/or other options? I have not yet explored those options. Sorry if this is a duplicate. my.data <- read.table(text= " r1 r2

means and SD for columns in a dataframe with NA values

喜欢而已 提交于 2019-12-01 20:46:22
I'm trying to calculate the mean and standard deviation of several columns (except the first column) in a data.frame with NA values. I've tried colMeans , sapply , etc., to create a loop that runs through the data.frame and then stores means and standard deviations in a separate table but keep getting a "FUN" error. any help would be great. Thanks a sapply(df, function(cl) list(means=mean(cl,na.rm=TRUE), sds=sd(cl,na.rm=TRUE))) col1 col2 col3 col4 col5 means 3 8 12.5 18.25 22.5 sds 1.581139 1.581139 1.290994 1.707825 1.290994 as.data.frame( t(sapply(df, function(cl) list(means=mean(cl,na.rm

How to void type conversion in R's apply (bit64 example)

六眼飞鱼酱① 提交于 2019-12-01 20:28:48
I am using the bit64 package in some R code. I have created a vector of 64 bit integers and then tried to use sapply to iterate over these integers in a vector. Here is an example: v = c(as.integer64(1), as.integer64(2), as.integer64(3)) sapply(v, function(x){is.integer64(x)}) sapply(v, function(x){print(x)}) Both the is.integer64(x) and print(x) give the incorrect (or at least) unexpected answers (FALSE and incorrect float values). I can circumvent this by directly indexing the vector c but I have two questions: Why the type conversion? Is their some rule R uses in such a scenario? Any way

read.xls - read in variable-length list of sheets, with their names

时光怂恿深爱的人放手 提交于 2019-12-01 16:25:33
问题 Given several .xls files with varying number of sheets, I am reading them into R using read.xls from the gdata package. I have two related issues (solving the second issue should solve the first): It is unknown ahead of time how many sheets each .xls file will have, and in fact this value will vary from one file to the next. I need to capture the name of the sheet, which is relevant data Right now, to resolve (1), I am using try() and iterating over sheet numbers until I hit an error. How can

Separate contents of field

▼魔方 西西 提交于 2019-12-01 14:07:18
I'm sure this is very simple, and I think it's a case of using separate and gather. I have a single field in a dataframe, authorlist,an edited export of a pubmed search. It contains the authors of the publications. It can, obviously, contain either a single author or a collaboration of authors. For example this is just a selection of the options available: Author Drijgers RL, Verhey FR, Leentjens AF, Kahler S, Aalten P. What I'd like to do is create a single list of ALL authors so that I'd have something like Author Drijgers RL Verhey FR Leentjens AF Kahler S Aalten P How do I do that? I

Retrieving sentence score based on values of words in a dictionary

删除回忆录丶 提交于 2019-12-01 06:33:49
Edited df and dict I have a data frame containing sentences: df <- data_frame(text = c("I love pandas", "I hate monkeys", "pandas pandas pandas", "monkeys monkeys")) And a dictionary containing words and their corresponding scores: dict <- data_frame(word = c("love", "hate", "pandas", "monkeys"), score = c(1,-1,1,-1)) I want to append a column "score" to df that would sum the score for each sentence: Expected results text score 1 I love pandas 2 2 I hate monkeys -2 3 pandas pandas pandas 3 4 monkeys monkeys -2 Update Here are the results so far: Akrun's methods Suggestion 1 df %>% mutate(score

handling NA values in apply functions returning more than one value

梦想与她 提交于 2019-12-01 05:29:13
问题 I have dataframe df with two columns col1 , col2 , includes NA values in them. I have to calculate mean , sd for them. I have calculated them separately with below code. # Random generation set.seed(12) df <- data.frame(col1 = sample(1:100, 10, replace=FALSE), col2 = sample(1:100, 10, replace=FALSE)) # Introducing null values df$col1[c(3,5,9)] <- NA df$col2[c(3,6)] <- NA # sapply with return a value for a function stat <- data.frame(Mean=numeric(length = length(df)), row.names = colnames(df))

Retrieving sentence score based on values of words in a dictionary

半世苍凉 提交于 2019-12-01 03:20:55
问题 Edited df and dict I have a data frame containing sentences: df <- data_frame(text = c("I love pandas", "I hate monkeys", "pandas pandas pandas", "monkeys monkeys")) And a dictionary containing words and their corresponding scores: dict <- data_frame(word = c("love", "hate", "pandas", "monkeys"), score = c(1,-1,1,-1)) I want to append a column "score" to df that would sum the score for each sentence: Expected results text score 1 I love pandas 2 2 I hate monkeys -2 3 pandas pandas pandas 3 4