If you want to drop a sequence of variables in the data frame, you can use. For example if you wanted to drop var2, var3, and all variables in between, you'd just be left with var1: df2% select(-c(var2:var3) ) df2 # var1 #1 0.75879754 #2 0.31168919 #3 0.03983268. Drop a variable (column) Note: axis=1 denotes that we are referring to a column, not a row.
A useful way to remove a whole set of named-alike objects: rm(list = lsgrep('^tmp', ls))thereby removing all objects whose name begins with the string 'tmp'.Edit: Following Gsee's comment, making use of the pattern argument: rm(list = ls(pattern = '^tmp'))Edit: Answering Rafael comment, one way to retain only a subset of objects is to name the data you want to retain with a specific pattern. For example if you wanted to remove all objects whose name do not start with paper you would issue the following command: rm(list = grep('^paper', ls, value = TRUE, invert = TRUE)). If you just want to remove one of a group of variables, then you can create a list and keep just the variable you need. The rm function can be used to remove all the variables apart from 'data'. Here is the script: 0-data1-data12-data23-data3#check variables in workspacelsrm(list=setdiff(ls, 'data'))#check remaining variables in workspace after deletionls#note: if you just use rm(list) then R will attempt to remove the 'list' variable.list=setdiff(ls, 'data')rm(list)ls.
If you're using RStudio, please consider never using the rm(list = ls) approach!. Instead, you should build your workflow around frequently employing the Ctrl+ Shift+ F10 shortcut to restart your R session.
This is the fastest way to both nuke the current set of user-defined variables AND to clear loaded packages, devices, etc. The reproducibility of your work will increase markedly by adopting this habit.See on Rstudio community for (h/t @kierisi) for a more thorough discussion (the main gist is captured by what I've stated already).I must admit my own first few years of R coding featured script after script starting with the rm 'trick' - I'm writing this answer as advice to anyone else who may be starting out their R careers.of course there are legitimate uses for this - much like attach - but beginning users will be much better served (IMO) crossing that bridge at a later date.
Pretending it’s a matrixIf you want to extract values from a data frame, you can just pretend it’s a matrix and start from there. You can use index numbers, names, or logical vectors for selection, like you would with matrices.
For example, you can get the number of baskets scored by Geraldine in the third game like this: baskets.df“3rd”, “Geraldine”1 2Likewise, you can get all the baskets that Granny scored using the column index, like this: baskets.df, 11 12 4 5 6 9 3Or, if you want this to be a data frame, you can use the argument drop=FALSE exactly as you do with matrices: str(baskets.df, 1, drop = FALSE)‘data.frame’: 6 obs. Of 1 variable:$ Granny: num 12 4 5 6 9 3Note that, unlike with matrices, the row names are dropped if you don’t specify the drop=FALSE argument.
Putting your dollar where your data isAs a careful reader, you noticed already that every variable is preceded by a dollar sign ( $) in the output from str. R isn’t necessarily pimping your data here — the dollar sign is simply a specific way for accessing variables. To access the variable Granny, you can use the dollar sign like this. baskets.df$Granny1 12 4 5 6 9 3So you specify the data frame, followed by a dollar sign and then the name of the variable. You don’t have to surround the variable name by quotation marks (as you would when you use the indices). R will return a vector with all the values contained in that variable. Note again that the row names are dropped here.With this dollar-sign method, you can access only one variable at a time.
If you want to access multiple variables at once using their names, you need to use the square brackets.