One row to many rows [duplicate] - r

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

Related

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?

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

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

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

Extract information from string with dots as separator in R [duplicate]

This question already has answers here:
How to use the strsplit function with a period
(3 answers)
Closed 5 years ago.
I apologize for possible similar questions, but I just can't find the solution for my problem. So, I have a string with three parts, separated by “.”, for example:
a <- "XXX.YY.ZZZ"
(the length of strings differ, it could also be a <- "XXXX.Y.ZZ", but the three parts are always separated by the two “.”.
I solved the problem for the first part:
library(stringi)
stri_extract(a, regex='[^.]*')
[1] "XXX"
Appreciate your help.
hello you can use strsplit as follows
strsplit(a,"\\.")[[1]]

R referring to dataframe columns by label to delete them [duplicate]

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

Resources