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

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

Related

Is there an easy way to order a large number of columns without using dplyr::select() in r? [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 3 years ago.
Improve this question
I'm working with a very large dataset with lots of columns (400+) and every time I create a new variable or add a new one I have to reorder it. I want it ordered so that all the related variables remain together so I've been using dplyr::select() to reorder things. Yet there are times when I have to go back into my script very early on and add a new variable. When I run the whole code after that, there tends to be one or two variables I forgot to put into preceding select() functions so it goes missing.
I use select() because selecting all the columns between two variables and referencing them by name is super easy (eg, Vfour:Vthreefifty). Do you have any tips for reordering datasets with lots of columns?
Given no reproducible example but using your 2 column names:
df %>%
select(., starts_with('V'))
You can then chain starts_with as needed.
Other options include:
ends_with, contains, matches

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.

for loop to read same name with different number [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 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.

Resources