Reorder single columns in R, without specifying all columns [duplicate] - r

This question already has answers here:
select() set position
(2 answers)
Closed 4 years ago.
I have an R data frame with many columns. On the right end of the frame I have created new columns. Now I would like to see this new column somewhere on the left. I usually work with dplyr.
Example:
This is what I have:
FirstName|LastName|Height|Width|Nationality
This is what I want:
FirstName|LastName|Nationality|Height|Width
Of course, I could do a
select(....)
but this way I would have to specify all(!) columns. I am looking for something like
select*(FirstName,LastName,Nationality)
without having to specify all, but only the first columns and leave the rest as it is.

Try this:
select(FirstName, LastName, Nationality, everything())
Or if you have many columns "on the left" that you don't want to name (only 2 in this example):
select(1:2, Nationality, everything())

Related

One row to many rows [duplicate]

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

r comparison operator is not working for a very simple code [duplicate]

This question already has answers here:
Dealing with spaces and "weird" characters in column names with dplyr::rename()
(1 answer)
R dplyr filter column with column name that starts with number
(1 answer)
Closed 2 years ago.
The filter in the first picture is clearly not working. None of the rows are filtered. However, in the second picture, there are clearly much less rows that satisfy the condition average value < good(blue) than the first picture.
Why is the code in the first picture not working and what may be a possible solution?

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

How can I do the equivalent of two VLookups between two tables? [duplicate]

This question already has answers here:
How to do vlookup and fill down (like in Excel) in R?
(9 answers)
How to join (merge) data frames (inner, outer, left, right)
(13 answers)
Closed 5 years ago.
I hope this isn't considered a duplicate question; I can't seem to figure it out. I have multiple tables that I'm trying to join together, by essentially doing a VLookup. That's how I would do it in Excel anyway.
I have 2 SQL data sets that are combined in this fashion.
Combined <- rbind(FGD, FNK)
In this Combined data set, I am trying to do a VLookup to the field named 'Category' and in another data set, named 'Category' I also have a field named 'Category'. This comes from an Excel file.
I tried this:
merge(Combined.Category, Category.Category, all=TRUE)
I just got this error.
Error in merge(Combined.Category, Category.Category, all = TRUE) :
object 'Combined.Category' not found
Isn't it like this? merge(Table1.Field1, Table2.Field2, all=TRUE)
Also, to make this a bit more complex, I want to do a VLookup to Category.Category and if no match is found, do another VLookup to Category.DES.
How can I do that? I'm pretty sure there is a way to do this, but I don't really know how to approach this kind of thing.
You've got a few issues here.
1) You access the columns of a data.frame with $, not .
You seem to have two data.frames - Combined and Category. To access the Category column in each, use Combined$Category and Category$Category.
2) merge() expects data.frames, not columns. So as PoGibas pointed out, you'd want to do
merge(Combined, Category, by="?")
where ? is the name of the column that is common in the two data sets. Based on your description, it doesn't sound like this would be "Category" but there's not enough information for me to tell this. Let's say you have an ID column in both Combined and Category. Then you would do
merge(Combined, Category, by="ID")
3) Assuming you do have some id column or columns to link the two data sets, then the merged result has all columns from both data sets in it. Columns that appear in both data sets get a suffix added to them. The ones from the first data set will have "original_name.x" and the ones from the second will have "original_name.y"
Then you can make a new column that gets the value of Category$Category if it exists and the value of Category$DES if not:
mergeddata <- merge(Combined, Category, by="ID")
mergeddata$desired_value <- ifelse(is.na(mergeddata$Category.y),
mergeddata$Category.y, mergeddata$DES)
If you don't actually have NAs in your data, then you might need to change the condition in ifelse() to check for empty strings or some other value that indicates there isn't a valid Category.

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