sapply

Using sapply on vector of POSIXct

青春壹個敷衍的年華 提交于 2019-12-05 04:25:33
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 problem. So I'm stumped at the moment about what the problem is. Do I need to do something special with

Convert a list of lists to a character vector

[亡魂溺海] 提交于 2019-12-05 02:11:48
I have a list of lists of characters. For example: l <- list(list("A"),list("B"),list("C","D")) So as you can see some elements are lists of length > 1. I want to convert this list of lists to a character vector, but I'd like the lists with length > 1 to appear as a single element in the character vector. the unlist function does not achieve that but rather: > unlist(l) [1] "A" "B" "C" "D" Is there anything faster than: sapply(l,function(x) paste(unlist(x),collapse="")) To get my desired result: "A" "B" "CD" You can skip the unlist step. You already figured out that paste0 needs collapse =

How to subset a list using another list?

大憨熊 提交于 2019-12-04 20:02:40
I have two lists and I want to subset data in a list using another list. Say, I have lists called mylist and hislist : mylist <- list(a = data.frame(cola = 1:3, colb = 4:6), b = data.frame(cola = 1:3, colb = 6:8)) > mylist $a cola colb 1 1 4 2 2 5 3 3 6 $b cola colb 1 1 6 2 2 7 3 3 8 > and hislist hislist <- list(a = 5:6, b = 7:8) > hislist $a [1] 5 6 $b [1] 7 8 I tried to subset mylist using lapply function: lapply(mylist, function(x) subset(x, colb %in% hislist)) #or lapply(mylist, function(x) x[x$colb %in% hislist,]) but these does not work. How to solve this issue? The most straightforward

R: Webscraping a list of URLs to get a DataFrame

青春壹個敷衍的年華 提交于 2019-12-04 19:25:45
I can see the correct data, but cannot put it on a Data Frame (It appears as a list of elements). I think the problem is my understanding of the apply family functions. Any hint is welcome. Here is a similar question, but I think it is better to post mine as it contains more details: Webscraping content across multiple pages using rvest package library(rvest) library(lubridate) library(dplyr) urls <- list("http://simple.ripley.com.pe/tv-y-video/televisores/ver-todo-tv", "http://simple.ripley.com.pe/tv-y-video/televisores/ver-todo-tv?page=2&orderBy=seq", "http://simple.ripley.com.pe/tv-y-video

Apply a function to each row in a data frame in R [duplicate]

主宰稳场 提交于 2019-12-04 09:56:04
问题 This question already has answers here : Closed 7 years ago . Possible Duplicate: how to apply a function to every row of a matrix (or a data frame) in R R - how to call apply-like function on each row of dataframe with multiple arguments from each row of the df I want to apply a function to each row in a data frame, however, R applies it to each column by default. How do I force it otherwise? > a = as.data.frame(list(c(1,2,3),c(10,0,6)),header=T) > a c.1..2..3. c.10..0..6. 1 1 10 2 2 0 3 3 6

Separate contents of field

天大地大妈咪最大 提交于 2019-12-04 02:32:22
问题 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

R: Apply function on specific columns preserving the rest of the dataframe

心不动则不痛 提交于 2019-12-03 12:16:00
I'd like to learn how to apply functions on specific columns of my dataframe without "excluding" the other columns from my df. For example i'd like to multiply some specific columns by 1000 and leave the other ones as they are. Using the sapply function for example like this: a<-as.data.frame(sapply(table.xy[,1], function(x){x*1000})) I get new dataframes with the first column multiplied by 1000 but without the other columns that I didn't use in the operation. So my attempt was to do it like this: a<-as.data.frame(sapply(table.xy, function(x) if (colnames=="columnA") {x/1000} else {x})) but

Dollar operator as function argument for sapply not working as expected

China☆狼群 提交于 2019-12-03 10:55:58
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? NGaffney From what I've been able to determine it's a combination of two things. First, the second element of $ is matched

Apply function to each column in a data frame observing each columns existing data type

时光总嘲笑我的痴心妄想 提交于 2019-12-03 06:28:24
问题 I'm trying to get the min/max for each column in a large data frame, as part of getting to know my data . My first try was: apply(t,2,max,na.rm=1) It treats everything as a character vector, because the first few columns are character types. So max of some of the numeric columns is coming out as " -99.5" . I then tried this: sapply(t,max,na.rm=1) but it complains about max not meaningful for factors . ( lapply is the same.) What is confusing me is that apply thought max was perfectly

Repeating a user-defined function using replicate() or sapply()

南楼画角 提交于 2019-12-03 05:15:55
问题 I have defined a custom function, like this: my.fun = function() { for (i in 1:1000) { ... for (j in 1:20) { ... } } return(output) } which returns an output matrix, output , composed by 1000 rows and 20 columns. What I need to do is to repeat the function say 5 times and to store the five output results into a brand new matrix, say final , but without using another for-loop (this for making the code clearer, and also because in a second moment I would like to try to parallelize these