4,058 questions
1
vote
1
answer
98
views
R lapply column not found
I wrote the codes below. First, I defined a function called 'dist_function' and then run this function in lapply.
While the column 'cyl' is fixed, lapply function returns x does not exist. Don't know ...
2
votes
3
answers
80
views
How do I cross-reference listA with a named listB and extract the names of listB for which members of listA are within?
I am writing a function in R which allows for Differential Gene Expression data to be plotted while grouping different genes based on a user's interest. For example, GO terms contain genes that are ...
5
votes
2
answers
103
views
`lapply()` loses the classes of its elements
I have a list of objects. If I separately ask for the class, I get:
> for (i in 1:2) print(class(tmp[[i]]))
[1] "emmGrid"
attr(,"package")
[1] "emmeans"
[1] "...
0
votes
2
answers
99
views
How to use R to output dataset of excel workbook names and sheet names from all excel files in a folder
I would like to create a dataset in the format below of workbook names and sheet names of all the excel files in a folder.
workbook sheetname
a aphid1
a aphid2
b ...
4
votes
3
answers
99
views
Split up dataframe into list of dataframes based on breaks in one column
I have a dataframe with a list of consecutive measurements of temperature over a period of time. I would like to split these up into separate dataframes based on when the temperature is consecutively ...
7
votes
4
answers
163
views
Append each element of a list to each list of a list of lists in R
I would like to append each element of a list to each list of a list of lists.
l1 <- list(list("a", "b", "c"),
list("d", "e", "f&...
0
votes
1
answer
73
views
lapply returns the same value for every data frame element
I created a function that splits a string by ":" and takes the first element, which is the information I need from a vcf:
remove_semicolon = function(x){
newstr = strsplit(x,":"...
2
votes
1
answer
91
views
Faster than R's `simplify2array`?
I often have code that looks like
rbind.oc.by <- function (indata, INDICES, FUN, ...) {
result <- by( indata, INDICES, FUNIN, ... )
t(simplify2array(result))
}
mynewdata <- rbind.oc....
2
votes
1
answer
64
views
Finding earliest date within a column
I am trying to find the earliest date in a column split by a delimiter ("\n" in this case), and creating a new column.
data.frame(x = c("2023-1-2\n2034-02-10", "2023-1-2\n2001-...
0
votes
1
answer
60
views
Apply function to one element in a list of lists
I have mylist a list of lists as follows:
list1 <- list(label = c(2,8,9,5,9,10,0,1), code = c(585,652,756,255,365,123), name = c("cow", "sheep", "fox", "dog",...
0
votes
1
answer
50
views
Create new data frames for each label in a variable [duplicate]
I have many labels under a variable for which I want to filter on that label and create new separate data frames for each label. I’m interested in the best way of doing this. Currently I am trying to ...
0
votes
1
answer
40
views
How to change the same column in multiple dataframes to a unique identifier?
I am trying to place a unique ID in the Site.Name column of 8 similar dfs using a list of names. I tried
site<-c("Smith","Sledd Creek","Crooked Creek","Duncan&...
0
votes
3
answers
136
views
How to find the mean number of consecutive days using dates in R?
I am working with climate data that I have added dates to and subsetted down to just including the data that has the temperature above the 90th percentile for 3 or more days.
set.seed(12368)
A <- ...
1
vote
2
answers
97
views
How to paste a character variable in a lapply function?
I asked participants to write three words and to assign a number to each word. Some words have been written by several participants, other words by only one participant.
Now I'm trying to create a set ...
1
vote
2
answers
99
views
How to create a subset of multiple data frames in a list that only contains the rows that have consecutive numbers in one of the columns?
I have a list of data frames that each have year, month, day, and temperature columns along with some other stuff. I have already figured out (with stack overflow help thanks guys) how to subset the ...
0
votes
1
answer
68
views
How to deal with ´append´ like functions in lapply?
I have a function (more specifically prophet::add_regressor()) which appends values to an object. I recreated a similar function to make sure my point is clear:
add_to_a_list = function(object, ...
0
votes
4
answers
71
views
How to use lapply on strings so that they end up grouped together?
The actual issue: I'm trying to rearrange a bunch of names, street addresses, states, etc. into a nice list of addresses without any weird bracketed [row numbers], quotation marks, awkward breaks, etc....
3
votes
2
answers
109
views
Improve processing time of applying a function over a vector and grouping by columns
I am trying to apply a function over data.table columns, and grouping by columns value.
I am using the lapply fuction, but my script is quite slow.
To give some context, I am working of probability ...
0
votes
1
answer
500
views
How to check if elements of one list are also elements of another list in R
Let's assume I have two lists in R, a long one and a shorter one, for example:
list1 = list(571,572,573,574,561,562,563,564,595,570,571,573)
list2 = list(c(571,564,565,600))
Please note that numbers ...
0
votes
1
answer
44
views
Is there a way to use data.frame to iterate through a list of texts in R?
I used readLines from gutenbergr to read my list of legal cases into R. The code looks like this:
my_list <- list.files(path = "C:\\Users\\Ben Tice\\Documents\\R Stuff\\UW Job\\Cases\\data\\...
0
votes
0
answers
34
views
I have a list that contains 12 elements. I would like to make a function that subset the list based on a sequence every nth element
I have a list containing 12 data frames. I would like to subset/split the list after every third elements based on a sequence. And make a new data frame of the new subsetted list .
# my_list
...
0
votes
1
answer
45
views
r nested lapply with two opposite conditions
I have a dataset which is like this.
library(dplyr)
set.seed(505)
tempdf1 <-
data.frame(
y = rnorm(400),
x1 = rnorm(400),
x2 = rnorm(400),
x3 = sample(1:5, 40, replace = TRUE),
...
1
vote
2
answers
99
views
Iterate along the two vectors of different length using map()
I have two vectors of unequal length. One vector is a list of dataframes and the other vector is made up of unique values. How can I use map() to iterate the vectors over a custom function? Dummy data ...
0
votes
2
answers
65
views
sapply extra things being printed
I have a vector in R which is like this:
vec <- c('abc','def')
Here is my dummy input data frame:
Gen value
abc 12
def 34
abc 12
abc 13
def 1
abc 4
abc 6
ghi 23
I am using sapply on this vec like ...
2
votes
3
answers
191
views
Using the relocate function with lapply
I have 4 data frames that I created to be a list
dfs <- list(bio_w, bio_k, demo_w, demo_k)
Now what I want to do is move my id column to the very front. I spent hours trying to get it to apply to ...