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?
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)
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))
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)
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]