Specific code from SAS to 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 5 years ago.
Improve this question
I am trying to write this (SAS) comand in R. x is a variable with this specific format: j61915035t
x1 = trim(upcase(substr(x,1,1)));
I really appreciate what you are doing in this site!

You want to remove leading/trailing blanks from a character string that is the uppercase first letter of the string x. So this should do it.
library(stringr)
x1 = str_trim(str_to_upper(str_sub(x,1,1)))

Related

How to add parentheses between numbers? I know how to add dashes, are they doing the same way? [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 1 year ago.
Improve this question
how to add parentheses between numbers? I know how to add dashes, are they doing the same way? like 123456789 to (123)456789 in R programming?
sub("(\\d{3})", "(\\1)", 123456789)
[1] "(123)456789"

Create new column from two column with logic in R? [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 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.

Rounding a very small number in character format [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 5 years ago.
Improve this question
I have a very small number like 1.466013e-65,
I want to round it to 3 decimals left while keeping all the 0s, here is the code I used:
round(1.466013e-65,3)
this will get me 0, but I want it to be 0.000.
How to do it?
If you are fine with character, then you can do it using:-
sprintf("%.3f", round(1.466013e-65, 3))
[1] "0.000"

R How to convert data in Matrix? Factor to Integer? [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 6 years ago.
Improve this question
I am having trouble operating Matrixs.
First, I want to make factor data to integer so i can operate.
Second, The First col, which shows date, should be factor. How can i change?
Try this:
d <- data.frame(1:10, letters[1:10])
data.matrix(d)
Also you can try this too :
m = matrix(scan("file.csv", what=numeric(), skip=1))
skip=1 to skip a header line.
Hope this will help you

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