I have a key value pair as below which is in form of string
> data$Frequency 1 A1:6,B1:4,AA1:3 2 BB1:2,AAA1:1 3 BBB1:1,A2:1,C2:3 4 D1:1 > class(data$Frequency) "character" FreqIndex<-which(colnames(data)=="Frequency") freq_string<-paste(as.character(unlist(data[FreqIndex])),collapse=",") print(freq_string) >"A1:6,B1:4,AA1:3,BB1:2,AAA1:1,BBB1:1,A2:1,C2:3,D1:1"
How can i find keys which have value greater than 2
A1,B1,AA1,C2
Here is another variation of strsplit
where we split by :
or ,
, then assuming that there are always key/value pairs, use the logical index recycling to subset the numeric values, check whether it is greater than 2 and subset the non-numeric elements
v1 <- unlist(strsplit(data$Frequency, "[:,]"))
v1[c(TRUE, FALSE)][as.numeric(v1[c(FALSE, TRUE)]) >2]
#[1] "A1" "B1" "AA1" "C2"
Or we can use tidyverse
options
library(tidyverse)
filtered <- data %>%
separate_rows(Frequency, sep=",") %>%
separate(Frequency, into = c('group', 'freq'), convert = TRUE) %>%
filter(freq>2) %>%
pull(group)
filtered
#[1] "A1" "B1" "AA1" "C2"
condition <- paste(shQuote(filtered, type="cmd"), collapse=", ")
condition
#[1] "\"A1\" , \"B1\" , \"AA1\" ,\"C2\""
x <- "A1:6,B1:4,AA1:3,BB1:2,AAA1:1,BBB1:1,A2:1,C2:3,D1:1" #create the string
y <- sapply(strsplit(x, ","), strsplit, ":") #split the string into single keys and values
dat <- data.frame(key = rep(NA, 9), value = rep(NA, 9)) #prepare empty dataframe
## fill dataframe
for(i in 1:length(y)){
dat[i, 1] <- y[[i]][1]
dat[i, 2] <- y[[i]][2]
}
gives you
> dat
key value
1 A1 6
2 B1 4
3 AA1 3
4 BB1 2
5 AAA1 1
6 BBB1 1
7 A2 1
8 C2 3
9 D1 1
and now dat$key[dat$value > 2]
gives you
> dat$key[dat$value > 2]
[1] "A1" "B1" "AA1" "C2"
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.