for loop to read same name with different number [closed] - r

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I have several data frames named kpi1,kpi2,kpi3,... .I want to create one data frame that includes some rows of each of these data frames (that contain certain values). I have the code to extract what I want from each of them, but I would like to write a loop to select each of these data frames at each iteration, perform the extraction, and go to the next file. So in the first iteration I can do stuff with kpi1, second iteration with kpi2, and so on.
Thank you in advance

You can use mget to collect them into a list, then use lapply or sapply to process each one.
Or you can use get in the loop. Use either sprintf or paste0 to create the list of names.
out1 <- lapply( mget( sprintf( 'kpi%d', 1:25 ) ), function(df) df[1:3, ] )
out2 <- do.call(rbind, out1)
In general it is better to keep multiple data frames that you will be iterating over like this in a single list rather than have each one in its own data frame in the global environment.

Related

Automatically extracting multiple data frames from the list in R [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
I have a list with 52 elements. Half of the list elements are data frames with 6 columns and different number of rows. The remaining half of the elements are just lists each containing character and integer elements. I want to get all data frames from the list using custom made functions. Ideally, I want to extract all the data frames from the list as separate objects with unique names. I tried to automate it without any success using loops and if/else functions. The thing is, I need to automate this process as I will produce more lists containing 50 to 60 elements. Any suggestion would be most appreciated.
with l as your list:
classes.in.list=lapply(l,class) # find classes in your list
idx=which(classes.in.list=="data.frame") # find indices of data.frames
mydfs=l[idx] # subset your list
data.table::rbindlist(mydfs,idcol=T) # fast rbind with ID column (install data.table library)

How to call several objects (data frame) in R so I only have to run a function once? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
I have several data frames in my environment, beginning with SPECIALITY:
I would like to be able to only call the data frames once in my self-defined functions (possibly with an apply function), instead of having to run a line of code for each data frame like so:
I was thinking of combining the data frames into a list, but I'm not sure how I would go about doing this, or whether that would be the most efficient method.
Storing them into a list is an excellent idea, you can do it this way:
new_list <- mget(ls(pattern="^SPECIALTY"))
And then use lapply on it with the function of your choice.
If you want to clean up your workspace after you've put them in a list, run :
rm(list = ls(pattern="^SPECIALTY")))
To go further you might want to challenge why you got them in separated tables to start with, maybe it's because you've done something like:
SPECIALTY2014_Q1 <- read.csv("SPECIALTY2014_Q1.csv")
SPECIALTY2014_Q2 <- read.csv("SPECIALTY2014_Q2.csv")
...
In this case you could have done the following to store everything in a list from the start:
lapply(paste0("SPECIALTY", c("2014_Q1", "2014_Q2"),".csv"), read.csv)

R - subsetting a list [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I am quite new to R and it would be a big help to find a way to do this.
I have a list of values(just one Column and around 16000 values) and I have to break this list up into smaller packets of 1000 values each. Then save each list as a csv file.
Is there a way of doing this is R?
Thank you in advance,
Dgupta
Something like this:
data <- as.data.frame(list)
groups <- split(1:nrow(data), ceiling(seq_along(1:nrow(data)/1000))
for (i in 1:length(groups)){write.csv(data[groups[1,],file=paste(i,'csv'))}
You can split the vector up by using a colon symbol in the vector index. For example:
> x <- c(10,20,30,40)
> x[1:2]
[1] 10 20
you can then write a vector to a csv by using the write.csv()function. One way to approach this is to write loop that gets 1000 items at a time from the vector and writes it to a csv.

R: Remove a row from all data.frames in global environment [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I have over 200 data.frames in my global environment. I would like to remove the first row from each data.frame, but I am not sure how.
Any help will be appreciated please let me know if further information is needed.
This will list all the data frames in your environment, remove the first row from each, and organize them into a list of data frames. Generally, better practice to have them in a list so you can more easily apply functions across them and access them.
df <- lapply(ls(), function(x) get(x)[-1,])
Update: good idea to check if objects are in fact data frames and only work with those. First we create a logical vector listing dataframes, then combine them into a list and remove the first row of each.
dfs = sapply(ls(), is.data.frame)
lapply(mget(names(dfs)[dfs]), "[", -1, , drop = FALSE)
thanks to comments for finding my error and providing more efficient solutions

Simple R function with variable number of arguments [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I am working on a project converting a bunch of stata code to R to perform data cleaning, and one of the things I'm trying to do is to write a single R function that cleans all of my Yes/No variables that were previously coded as (Yes = 1, No = 2) to standard dummy variables.
The thing is that the number of variables that need to be cleaned by this function will constantly be changing. So my guess is that the function will need to take as its arguments (1) the dataset/dataframe with all the variables, and (2) the list of variables that need to be cleaned.
Any help on this would be greatly appreciated, as I'm pretty new to R.
Thanks!
You could try this:
example <- data.frame(sex=runif(10),q1=rep.int(c(1,2),5),q2=rep.int(c(2,1),5))
yesno <- function(data, variables) {
data.new <- data
data.new[,names(data) %in% variables] <- -data[,names(data) %in% variables]+2
return(data.new)
}
example
yesno(example, c("q1","q2"))
sapply(data, function(x) {-x+2})
data contains your columns of 1, 2. The anonymous functions turns all Yes/1 into 1, and No/2 into 0.

Resources