Create new column from two column with logic in R? [closed] - r

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
The community is reviewing whether to reopen this question as of 2 years ago.
Improve this question
Looking for other way than ifelse.
How to create NewColumn like this:

As displayed in your picture, you want to paste together two columns. Assuming your dataframe is called df, you can do:
df$NewColumn <- paste(df$Column2,"",df$Column1)
Which will get you the outcome in the picture.

Related

How is it possible to add 15 to every figure in a column, in a tibble? [duplicate]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
I’m new to R and I’m trying to add 15 to every figure in my dataset for a specific column and was wondering how it’s possible to this. Any help would be much appreciated, thanks.
Asssuming you have a data.frame df with a column col that you want to increase:
df$col <- df$col + 15
No loop required, the fundamental objects in R are vectors.

How to order a data.drame with the variables' order in codebook [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
If I have a data.frame that contain the variables that listed in df$Var. Is it a way for me to reorder my data col in the order of df$Var?
The df is looks like this:
Is it a way I can sort dt so the col in dt will in the order of var in df?
Thanks.
We can use factor with levels specified as the values in 'df1$Var'
dt1[order(factor(names(dt1), levels = df1$Var))]
Or use match
dt1[order(match(names(dt1), df1$Var))]

I need help transforming one dataframe to another [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
I am trying to understand how to go about transforming dataframe in pic 1 into 2. How can i go about doing the transformation?
We can use gather with extract
library(tidyverse)
gather(df1, year, MatriculationRates, -1) %>%
extract(year, into = 'year', '[^0-9]+([0-9]+)')

R: Vector Group by Defined group [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I have a vector c("A","B","C",......) and a list list(c("A"),c("B","C"))
I want to get a vector c(1,2,2....)
Is there any function in some basic packages?
we can use merge
merge(stack(setNames(lst, seq_along(lst))), data.frame(values=v1))$ind

data.table complex manipulation with rows elimination [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
Data sample is below: I have 3million rows.
Date,id,type,qty
9/30/14,1,’A’,10
9/30/14,2,’Z’,12
9/30/14,3,’B’,15
9/30/14,1,’B’,20
9/30/14,1,’Z’,20
9/30/14,1,’A’,20
9/30/14,2,’B’,20
9/30/14,3,’B’,5
9/30/14,3,’A’,40
I want result as below:
Date,id,type,Qty
9/30/14,1,A,20
9/30/14,1,B,20
9/30/14,2,B,20
9/30/14,3,B,5
9/30/14,3,’A’,40
Logic is below: On the same date, pick the latest qty (from the later record) for each id and type.Ignore types y and Z.
DT[,.(Qty=last(qty)),by=.(Date,id,type)][type!='Z'][order(id)]

Resources