This question already has answers here:
How to drop columns by name in a data frame
(12 answers)
Closed 9 years ago.
An easy one I suppose though my searches have been pretty fruitless --
given
z=data.frame(X.39=rnorm(20),X.40=rnorm(20),X.51=rnorm(20))
the subsetting operation
z[,c('X.39','X.51')]
works. but
z[,-c('X.39','X.51')]
gives me
Error in -c("X.39", "X.51") : invalid argument to unary operator
why is that and how do I remove a set of columns using a list of column names?
EDIT
I know that I can always use
z[,!names(z) %in% c('X.39','X.51')]
but I'm looking for a lazier solution
EDIT2
Most of the discussion has been in the comment section but to close this off for good order, the gist of this is that a lazier solution (direct reference by name) is not possible. This appears to be designed in.
You could use setdiff function, but I can't say if its the most elegant solution:
z[, setdiff(names(z), c('X.39','X.51'))]
Related
This question already has answers here:
Split comma-separated strings in a column into separate rows
(6 answers)
Closed 4 months ago.
everyone. I have a one-to-multiple observation, but the "many observations" are in one row. I'd like to break it into many rows (as many as the size of the answer), identifying by the id, just like the image below.
I'll relate de "yes/no" answers to how the ones who like apple consumes it and how who doesn't, consumes it.
Imma doing all in R.
Thanks in advance.
Use separate_rows from ‘tidyr’:
result = data |> separate_rows(`How do you consume it?`)
I found the answer in the Tidyr cheat sheet, sorry to bother you haha
https://raw.githubusercontent.com/rstudio/cheatsheets/main/tidyr.pdf
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 an answer here:
How to change type of target column when doing := by group in a data.table in R?
(1 answer)
Closed 5 years ago.
Below I am doing the same operation in two ways. The first does not work, while the second does work. I am wondering why? I have not been able to find an answer to this question in data.table documentation or other places through google.
SOtable <- data.table(testInt=c(1:100))
SOtable[,testInt := as.double(testInt), by=1:nrow(SOtable)]
##Error in `[.data.table`(SOtable, , `:=`(testInt, as.double(testInt)), :
## Type of RHS ('double') must match LHS ('integer'). To check and coerce would impact performance too much for the fastest cases. Either change the type of the target column, or coerce the RHS of := yourself (e.g. by using 1L instead of 1)
SOtable[,testInt := as.double(testInt)]
The reason to try this is because I wanted to do some manipulation on a column in a big data.table for each row, but as soon as I use by I get the LHS/RHS error. But as I am typing this I am thinking: "Maybe I should have used some apply function for this instead?"
Answer by #Roland:
In the first example you replace an entire column so the variable type is not a factor.
When using by each value gets written into the column when calculated so if the types differ it will try to write (in this case) a double variable into a integer column, which is not going to work.
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]
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How to delete a row in R
I can't figure out how to simply remove row (n) from a dataframe in R.
R's documentation and intro manual are so horribly written, they are virtually zero help on this very simple problem.
Also, every explanation i've found here/ on google is for removing rows that contain strings, or duplicates, etc, which have been excessively advanced for my problem and lead me to introduce more bugs and get nowhere. I just want to remove a row.
Thanks in advance for your help.
fyi the list is in the variable eld, which has 5 columns and 33 rows. I would like to remove row 14. I initialized eld with the following command
eld <- read.table("election2012.txt")
so my desired result is
eldNew <- eld(minus row 14)
eldNew <- eld[-14,]
See ?"[" for a start ...
For ‘[’-indexing only: ‘i’, ‘j’, ‘...’ can be logical
vectors, indicating elements/slices to select. Such vectors
are recycled if necessary to match the corresponding extent.
‘i’, ‘j’, ‘...’ can also be negative integers, indicating
elements/slices to leave out of the selection.
(emphasis added)
edit: looking around I notice
How to delete the first row of a dataframe in R? , which has the answer ... seems like the title should have popped to your attention if you were looking for answers on SO?
edit 2: I also found How do I delete rows in a data frame? , searching SO for delete row data frame ...
Also http://rwiki.sciviews.org/doku.php?id=tips:data-frames:remove_rows_data_frame