Complex string vector creation from data frame values [duplicate] - r

This question already has an answer here:
Convert R vector to string vector of 1 element [duplicate]
(1 answer)
Closed 2 years ago.
I have the following dataframe which has only one column called Values and a list of string values:
Values
AA
AB
CG
DS
KI
Is there a simple way to create a string vector with each value separated by |?
The desired resulting output should look something like this:
Vector = "AA|AB|CG|DS|KI"
Cheers!

Sure, you simply use (assuming your data is called df):
paste0(df$Values, collapse = "|")

Related

operating R dataframe using variables for the column name [duplicate]

This question already has answers here:
Dynamically select data frame columns using $ and a character value
(10 answers)
Closed 8 months ago.
I want to store a column name in a variable and operate a dataframe based on that column name.
For example if a I have two columns named car_sales and airplane_sales. I have a variable var that a user sets to say car_sales. i then calculate a new column like so:
calc_col <- paste0(var,"_delta")
df$calc_col <- abs(df$var - lag(df$var ,12))
The var will change based on user input, so the resulting column will also change
How do I do this in R?
You could use:
df[[calc_col]] <- abs(df[[var]] - lag(df[[var]], 12))

is there a way to set all cells in a dataframe in the form of a vector as NA? [duplicate]

This question already has answers here:
R: Count number of objects in list [closed]
(5 answers)
Closed 2 years ago.
I have a dataframe in R, and I am trying to set all cells in the form of a vector, either c(1,2,3) or 1:2 to NA. Is there any easy way to do this?
You can use lengths to count number of elements in each value of column. Set them to NA where the length is greater than 1. Here I am considering dataframe name as df and column name as col_name. Change them according to your data.
df$col_name[lengths(df$col_name) > 1] <- NA

R reference filtering column by variable name [duplicate]

This question already has answers here:
Dynamically select data frame columns using $ and a character value
(10 answers)
Closed 4 years ago.
Is there a way to get a filtered data frame, like this:
data[data$Measure=="Baseline",]
using a variable Name for Measure, i.e. measVarName == "Measure"?
Thanks.
Double bracket notation lets you select variables using a character string stored in a variable:
measVarName <- 'Measure'
data[data[[measVarName]] == 'Baseline',]

Subsetting columns by a string variable returns NULL in R [duplicate]

This question already has answers here:
Dynamically select data frame columns using $ and a character value
(10 answers)
Closed 5 years ago.
I can subset a column named A from a data frame x_data using x_data$A.
But if I do
some_string<-'A'
x_data$some_string
I get NULL.
Can someone please explain why is it so. Thanks.
You can reference a data frame with the $ operator using a string literal, only a column. If you want to subset using a string, use the list syntax:
sub_df <- x_data[[some_string]]

Split data frame elements with semicolon in R [duplicate]

This question already has answers here:
Split delimited strings in a column and insert as new rows [duplicate]
(6 answers)
Split comma-separated strings in a column into separate rows
(6 answers)
Closed 6 years ago.
I've tried to create a function that replaces semicolon-containing elements in a dataframe column with splitted entries that are places on the bottom of the column, using basic R. The main purpose is to use this function with apply and make the addition whenever detecting an entry with semicolon.
The main problem with my code is that it returns the exact same data frame without any additional values.
> df
rs2480711
rs74832092
rs4648658
rs4648659
rs61763535
rs28733941;rs67677371
>x
"rs28733941;rs67677371"
function(x){
semiCols = length(unlist(strsplit(x, ";")))
elementsRs = unlist(strsplit(x, ";"))
if(semiCols>1){
for(i in 1:semiCols){
df = rbind(df, elementsRs[i])
}}}
I would also like to know how can I expand the code in order to split rows based on one value leaving all the others unchanged. For example, this
>df
0 rs61763535 T1
1 rs28733941;rs67677371 T2
will look like this
>df2
0 rs61763535 T1
1 rs28733941 T2
1 rs67677371 T2
If I understood correctly, this will work
unlist(strsplit(as.character(df$V1),split = ";"))
Again, I couldn't get you properly. But, maybe you are looking for this
apply(df,2,function(t) unlist(strsplit(as.character(t),split = ";")))

Resources