sapply

Dollar operator as function argument for sapply not working as expected

混江龙づ霸主 提交于 2020-01-12 06:57:21
问题 I have the following list test_list=list(list(a=1,b=2),list(a=3,b=4)) and I want to extract all elements with list element name a . I can do this via sapply(test_list,`[[`,"a") which gives me the correct result #[1] 1 3 When I try the same with Rs dollar operator $ , I get NULL sapply(test_list,`$`,"a") #[[1]] #NULL # #[[2]] #NULL However, if I use it on a single element of test_list it works as expected `$`(test_list[[1]],"a") #[1] 1 Am I missing something obvious here? 回答1: From what I've

Converting columns to character with sapply

蹲街弑〆低调 提交于 2020-01-07 08:25:11
问题 I have a dataframe test whose columns are factors class(test) [1] "data.frame" sapply(test, class) street city state "factor" "factor" "factor" If I try to convert these columns to character with sapply() , something goes wrong and I not sure why test <- as.data.frame(sapply(test, as.character)) sapply(test, class) street city state "factor" "factor" "factor" I would expect the output to be all character columns. Why are the columns not converting and how would one convert all factor columns

Subsetting neighboring fileds

梦想与她 提交于 2020-01-03 05:33:17
问题 I am trying to make a conditional subsetting which would include contemporary elements inside window of neighboring areas. For example, given the matrix Dat, where Species (SP), Area (AR) and Time (TM): SP AR TM A 2 2 B 2 2 C 1 4 F 3 2 B 5 3 E 3 2 D 2 1 I 1 4 H 3 2 E 2 4 D 3 5 B 1 2 How can I retrieve all the species co-occurring with species A in the same time, but within neighboring areas (in this case 1 and 3)? The desired output being: SP AR TM A 2 2 B 2 2 F 3 2 H 3 2 B 1 2 This is based

Using sapply on vector of POSIXct

这一生的挚爱 提交于 2020-01-02 02:35:10
问题 I have what may be a very simple question. I want to process a column of POSIXct objects from a dataframe and generate a vector of datetime strings. I tried to use the following sapply call dt <- sapply(df$datetime, function(x) format(x,"%Y-%m-%dT%H:%M:%S")) but to no avail. I keep getting the following error: > Error in prettyNum(.Internal(format(x, trim, digits, nsmall, width, 3L, : invalid 'trim' argument When I apply this function to a single POSIXct object from the column, I have no

How to subset from a list in R

纵然是瞬间 提交于 2019-12-29 16:30:13
问题 I have a rather simple task but haven't find a good solution. > mylist [[1]] [1] 1 2 3 4 5 6 7 8 9 10 [[2]] [1] "a" "b" "c" "d" "e" "f" "g" "h" "i" "j" "k" "l" "m" "n" "o" "p" "q" "r" "s" "t" "u" "v" "w" "x" "y" "z" [[3]] [1] 25 26 27 28 29 30 31 32 y <- c(3,5,9) I would like to extract from mylist the sub-elements 3,5, and 9 of each component in the list. I have tried, sapply[mylist,"[[",y] but not luck!, and others like vapply, lapply, etc.. Thanks in advance for your help Mauricio Ortiz

All possible combinations of elements from different bins (one element from every bin) [duplicate]

安稳与你 提交于 2019-12-25 20:00:32
问题 This question already has answers here : Generate list of all possible combinations of elements of vector (6 answers) Closed 3 years ago . I have a list, where each element is a set of numbers. Lengths of all sets are different: a <- list(1,c(2,3),c(4,5,6)) #> a #[[1]] #[1] 1 #[[2]] #[1] 2 3 #[[3]] #[1] 4 5 6 I'd like to get all possible combinations of one element from each set. In this example it should be: 1 2 4, 1 2 5, 1 2 6, 1 3 4, 1 3 5, 1 3 6 I feel that some combination of *apply

Store output of sapply into a data frame?

删除回忆录丶 提交于 2019-12-25 18:28:28
问题 how can I store the output of sapply() to a dataframe where the index value is stored in first column and its value in corresponding 2nd column. For illustration, I have shown only 2 elements here, but there are 110 columns in my data. "loan" is the data frame. cols <- sapply(loan,function(x) sum(is.na(x))) cols id 0 member_id 7 I want output as: var value id 0 member_id 7 I know that sapply() returns a vector, but when I print the vector, values are printed along with its some "index" e.g.,

Plot titles in R using sapply()

对着背影说爱祢 提交于 2019-12-25 06:58:44
问题 I want to make qqnorm plot out of every variable in my data frame using sapply(). This is what I've got so far: myfun=function(x) { c(qqnorm(x), qqline(x) ) } sapply(mydata, myfun) It works, however, I'd like each plot to have the respective variable name in the title of the plot. How is this done? Thanx a lot ;-) 回答1: In this case l_ply is more suitable because you just need to plot and therefore no output is needed. Based on @Henrik answer we have require(plyr) myfun <- function(x, data, ..

Regression of variables in a dataframe

北慕城南 提交于 2019-12-25 06:44:59
问题 I have a dataframe: df = data.frame(x1 = rnorm(50), x2 = rnorm(50), x3 = rnorm(50), x4 = rnorm(50)) I would like to regress each variable versus all the other variables, for instance: fit1 <- lm(x1 ~ ., data = df) fit2 <- lm(x2 ~ ., data = df) etc. (Of course, the real dataframe has a lot more variables). I tried putting them in a loop, but it didn't work. I also tried using lapply but couldn't produce the desired result either. Does anyone know the trick? 回答1: You can use reformulate to

applying sapply or other apply function instead of nested for loop for lists of data frames

女生的网名这么多〃 提交于 2019-12-25 04:22:32
问题 I have two data frames/ lists of datas 'humanSplit and ratSplit` and they are of the form > ratSplit$Kidney_F_GSM1328570 ratGene ratReplicate alignment RNAtype 1 Crot Kidney_F_GSM1328570 7 REV 2 Crot Kidney_F_GSM1328570 12 REV 3 Crot Kidney_F_GSM1328570 4 REV and > humanSplit$Fetal_Brain_408_AGTCAA_L009_R1_report.txt humanGene humanReplicate alignment RNAtype 53 ZFP28 Fetal_Brain_408_AGTCAA_L009_R1_report.txt 5 reg 55 RC3H1 Fetal_Brain_408_AGTCAA_L009_R1_report.txt 9 reg 56 IFI27 Fetal_Brain