例如,我有一个表格。
Field1 | Field2 | Field3 | Field4 |
---|---|---|---|
1 | A | Y | |
2 | B | Y | |
3 | B | N | |
4 | A | Y |
如果我有一个问询,我写了吗?
Select Case when Field1 = 1 and Field2 = A then Field3 Else Field4 End as NameOfField From Table
问 题:我怎么能用同样的语在RStudio上写同样的问题?
早孕
例如,我有一个表格。
Field1 | Field2 | Field3 | Field4 |
---|---|---|---|
1 | A | Y | |
2 | B | Y | |
3 | B | N | |
4 | A | Y |
如果我有一个问询,我写了吗?
Select Case when Field1 = 1 and Field2 = A then Field3 Else Field4 End as NameOfField From Table
问 题:我怎么能用同样的语在RStudio上写同样的问题?
早孕
如@tinazmu所建议的,您可使用<代码>case_ when from dplyr
#。 你在R.K.的询问相当于:
library(dplyr)
df |>
mutate(
NameOfField = case_when(Field1 == "1" & Field2 == "A" ~ Field3,
TRUE ~ Field4)
)
产出:
#> Field1 Field2 Field3 Field4 NameOfField
#> 1 1 A Y <NA> Y
#> 2 2 B <NA> Y Y
#> 3 3 B <NA> N N
#> 4 4 A Y <NA> <NA>
此外,由于你的询问只是对一个条件进行检查,我可以建议采用另一个方式,即<条码>。
df |>
mutate(
NameOfField = ifelse(Field1 == "1" & Field2 == "A",
Field3,
Field4)
)
# output
#> Field1 Field2 Field3 Field4 NameOfField
#> 1 1 A Y <NA> Y
#> 2 2 B <NA> Y Y
#> 3 3 B <NA> N N
#> 4 4 A Y <NA> <NA>
<>Data:
df <- read.table(text="Field1 Field2 Field3 Field4
1 A Y NA
2 B NA Y
3 B NA N
4 A Y NA", header=TRUE, colClasses="character")
I have monthly datasets excel files. And I want to combine those files into one. I wanted to see the movement of payments of my customer monthly side by side (see sample table) customer January ...
I was trying a seq() function for a loop in R studio and found that I had a problem with it. When I ask : 5.1 %in% seq(0,9, by = 0.01) (or even 0.1) R print FALSE. Only when I print the sequence, 5.1 ...
I m trying to run a simple R print function from Linux terminal but it failed due to loadNamespace error. I also cannot install that package from here because Rscript returns the same error. Rscript ...
I have a data frame called df that contains 2 columns; user and artist. head(df, 5) user artist 1 3bd73256-3905-4f3a-97e2-8b341527f805 betty ...
I installed RStudio version 0.98.507 and my R version is 2.14.1. My OS is ubuntu 12.04. When I try to start RStudio from the terminal I get the following error: rstudio: error while loading shared ...
I love RGoogleDocs and use it a lot. However, I don t like entering my password all the time. Obviously I could just type the password into the R script and would never have to enter it again. But ...
I have been using Rstudio a great deal these days but recently noticed that my commands are not being stored in the history any longer. I don t know then this started, but it might have been with the ...
I am running RStudio 0.92.38 on Windows Vista x64. I have hard time to increase the memory limit to what I have (12GB) by using memory.limit, such as memory.limit(size=12000) for ~ 12 GB. I always get ...