This question already has answers here:
Subset multiple rows with condition
(3 answers)
Closed 8 years ago.
Here is a trivialized example whose solution would help me greatly.
v.1<- c(5,8,7,2)
v.2<- c("hi", "hello", "hum", "bo")
df<- data.frame(v.1, v.2)
desired.values<- c("hi", "bo")
I would like all rows of the dataset where v.2 takes on one of the desired.values.
Desired output:
5 "hi"
2 "bo"
In my real dataset, v.2 has more than 10000 values and desired.values contains more than 2000 values.
You could try data.table
library(data.table)
setkey(setDT(df),v.2)[desired.values]
Or using base R methods
df[df$v.2 %in% desired.values,]
Or
df[grep(paste(desired.values, collapse="|"), df$v.2),]
Related
This question already has answers here:
Subset of rows containing NA (missing) values in a chosen column of a data frame
(7 answers)
Closed 2 years ago.
I would like to subset my data based on two conditions: if X is blank and if Y is blank.
Subsetting based on 1 condition is:
Blank_X <- Q4[is.na(Q4$X),]
How do I add a second condition to this?
Here is one way with subset
Blank_X <- subset(Q4,is.na(Q4$X) & is.na(Q4$Y))
with filter
Blank_X <- Q4 %>% filter(X!= NA & Y!=NA)
You can use & (and) to combine multiple conditions.
Blank_X <- Q4[is.na(Q4$X) & is.na(Q4$Y),]
This question already has answers here:
Split data frame string column into multiple columns
(16 answers)
How to split column into two in R using separate [duplicate]
(3 answers)
Closed 4 years ago.
I want to split a dataframe containing only 1 column with below values into two columns containing only the numeric values
1: [0.426321243245,0.573678756755]
2: [0.413189679382,0.586810320618]
I have tried different ways in R using dplr -starts_with,seperate etc but couldn't split the column into dataframe containing two seperate columns.
Can someone please help me with this?
Thanks,
I hope this will help you
newdf <- read.table(text = "column1
0.426321243245,0.573678756755
0.413189679382,0.586810320618
", header = T)
library(splitstackshape)
final <- cSplit(newdf, 'column1', sep=",", type.convert=FALSE)
This question already has answers here:
How to delete multiple values from a vector?
(9 answers)
Filter data frame rows based on values in vector
(4 answers)
Closed 4 years ago.
Sorry for the silly question, but I have a huge dataframe (called "totaldecade") with columns named:
Event.ID,Event.Date,CAMEO.Code
I want to delete all rows that have number ranges: 10:58, 90:145, 1011:1454, 160:166, 1661:1663, within the CAMEO:Code column.
I have tried:
totaldecade[with(totaldecade, !((CAMEO.Code %between% 10:58) |
(CAMEO.Code %between% 90:145) |
(CAMEO.Code %between% 1011:1454) | (CAMEO.Code %between% 160:166) |
(CAMEO.Code %between% 1661:1663))), ]
But doesn't seem to work.
Any help is appreciated!
Michelle
We get the ranges in a vector, use %in% to create the logical vector and negate (!) to change the FALSE elements to TRUE and viceversa
library(dplyr)
totaldecade %>%
filter(!CAMEO.Code %in% c(10:58, 90:145, 1011:1454, 160:166, 1661:1663))
Or using subset from base R
subset(totaldecade, !CAMEO.Code %in% c(10:58, 90:145, 1011:1454,
160:166, 1661:1663))
This question already has answers here:
Extracting specific columns from a data frame
(10 answers)
Closed 4 years ago.
in R programming, how do I subset a matrix so that I can skip columns in between? I only know how to do it continuously such as 1:4, but what if I want the first, second, and fourth colum
You can select specific columns as follows:
new_df <- x[,c(1,2,4)]# Select column 1,2 and 4
This question already has answers here:
Converting 1M to 1000000 elegantly
(3 answers)
Closed 6 years ago.
I have a data frame in R that has monetary values such as $25,000 and $2,000,000 entered as 25K and 2M respectively. The data frame is massive, so is there any way I can, for example, change all of the 2M's to 2000000's?
Try gsub() on the letters:
df$variableName <- gsub("M", "000000", df$variableName)
df$variableName <- gsub("K", "000", df$variableName)
and so forth...
Maybe convert the class when you're done class(df$variable) <- "numeric".