using mget function to create a list of dataframes [duplicate] - r

This question already has answers here:
Create dynamically named objects in a for loop in R and assign dynamic values
(2 answers)
How do I make a list of data frames?
(10 answers)
Closed 1 year ago.
I have several dataframes that I want to put in a list, so that they will be kept as separate dataframes (i.e. a list with 6 elements).
when I do it like this, it works:
df.list.fin <- list(data53,data54,data113,data117,data123,data127)
I want to make it automatic so I will not have to enter the numbers each time. I tried to do the following:
df.list.fin <- list(mget(paste0("data",video_number)))
Now I still get a list, but the dataframes within the list are not separable from one another. That is, I get one big list with one element.
Any idea how to correct this?

Related

When can I use brackets and when do I use the $-sign? [duplicate]

This question already has answers here:
The difference between bracket [ ] and double bracket [[ ]] for accessing the elements of a list or dataframe
(11 answers)
Dynamically select data frame columns using $ and a character value
(10 answers)
Closed 3 years ago.
I'm doing what seems to be a simple exercise. I'm supposed to create a histogram of the ages on the passengers on the titanic. The data frame "titanic" is already spacified. Since age is the sixth variable (I checked) I write the following:
hist(titanic[6])
This does not seem to work for some reason. R generates an error message and tells med that "'x' must be numeric".
(The variable age is indeed numeric, I checked this both with the str()-function and by executing the titanic[6]-command outside of the function.)
Meanwhile I can write it like this without any problems:
hist(titanic$Age)
Why can I use one and not the other? When, in general, can I use brackets and when do I have to use the $-sign?

Calling a certain column from a data frame inside an R function [duplicate]

This question already has answers here:
Pass a data.frame column name to a function
(8 answers)
Dynamically select data frame columns using $ and a character value
(10 answers)
Closed 4 years ago.
I was just trying to call a certain column inside an R function using the code below but it only returns a null value. I know it sounds stupid to call the column using the $ sign. I know it can be done by defining the targeted column directly in the input of the function. However, I just want to know why the code below is not working. Thanks in advance!
fn <- function(data, var){
return(data$var)
}
data(cars)
fn(cars, speed)

How do you the return column(s) number(s) based on class of said column? [duplicate]

This question already has answers here:
How to find all numeric columns in data
(2 answers)
Closed 4 years ago.
I have a list of 185 data-frames. I'm trying to edit them so each data frame only shows its numeric columns and also 2 specific, non-numeric ones.
I've had many issues with solving this, so I plan to use a for loop and find the column numbers of all numeric columns, use match to do the same for the two specific ones and then use c() to overwrite the data-frames.
I can pull the column number for the specific ones with
match("Device_Name",colnames(DFList$Dataframe))
successfully.
However, I cannot figure out how to return the numbers for all integer columns in a data-frame.
I have tried
match(is.numeric(colnames(DFList$Dataframe)),colnames(DFList$Dataframe))
and
match(class == "numeric",colnames(DFList$Dataframe),colnames(DFList$Dataframe))
to name a few, but now I am just taking wild stabs in the dark. Any advice would be welcome.
which(sapply(DFList$Dataframe,is.numeric))

R leaves out columns when using read.csv [duplicate]

This question already has answers here:
How can you read a CSV file in R with different number of columns
(5 answers)
Read a text file with variable number of columns to a list
(3 answers)
Closed 5 years ago.
I have a large comma delimted file that looks something like this:
LS_trap_10c,7C000000395C1641,trap10c_7C000000395C1641_150809.csv,c,5/30/2015,1800,25
LS_trap_10c,7C000000395C1641,trap10c_7C000000395C1641_150809.csv,c,5/30/2015,2000,24.5
LS_trap_10c,7C000000395C1641,trap10c_7C000000395C1641_150809.csv,c,5/30/2015,2200,24.5
LS_trap_10c,7C000000395C1641,trap10c_7C000000395C1641_150809.csv,c,5/31/2015,000,24
LS_trap_10c,7C000000395C1641,trap10c_7C000000395C1641_150809.csv,c,5/31/2015,200,23.5
LS_trap_10c,7C000000395C1641,trap10c_7C000000395C1641_150809.csv,c,5/31/2015,400,23.5,97
LS_trap_10c,7C000000395C1641,trap10c_7C000000395C1641_150809.csv,c,5/31/2015,600,23.5,98.5
As you can see the data vary (the bottom two instances have an extra column) and not all columns contain values. This data displays correctly in excel, but when I attempt to open it in RStudio with:
my_trap_dat = read.csv("path_to_file/la_selva_log.csv",(header = FALSE))
It does not contain all of the data- it leaves out the last column- so i have 7 columns instead of the 8 that are needed to display all data. The data in the last column seem to be just removed from the set when you load them into R.
I found this:
The number of data columns is determined by looking at the first five
lines of input (or the whole input if it has less than five lines), or
from the length of col.names if it is specified and is longer.
But I'm not sure how to implement any change that fixes my issue.
How can I make it so that all of my data is maintained in R?
This question is answered already on StackOverflow:
How can you read a CSV file in R with different number of columns
Read a text file with variable number of columns to a list
I'm sure you find more on stack overflow using the Search.
Quick example (given your exported CSV is not valid):
my_file = file("path_to_file/la_selva_log.csv")
my_data = strsplit(readLines(my_file), ",")
close(my_file)

Using a variable to extract information from a dataframe in R [duplicate]

This question already has answers here:
Dynamically select data frame columns using $ and a character value
(10 answers)
Closed 8 years ago.
I apologize since I'm sure this is an obvious issue, but I just can't seem to find the correct search terms to come up with the answer.
If I have a dataframe like this:
example<-cbind(rbind(1,2,3),rbind("a","b","c"))
colnames(example)<-c("a","b")
example<-as.data.frame(example)
And I want to extract the values from column a using a variable x,
x<-a
How do I go about this? When I try:
example$x
I get a null. How do I make this work?
I'm assuming a is a character:
x <- "a"
example[,x]

Resources