我想创建一个表或一个新的数据框,以显示原始数据框中每一行的特定值在另一个特定值之前的次数。例如,如果我具有以下数据框:
05-30 13:08:54.297 6116 6180 I ReactNativeJS: 'socket id in App.js : ', undefined
05-30 13:08:54.327 6116 6180 I ReactNativeJS: 'In App.js, token retrieved is : ', undefined
05-30 13:08:54.908 6116 6180 I ReactNativeJS: Running application "emps_app" with appParams: {"rootTag":1}. __DEV__ === true, development-level warning are ON, performance optimizations are OFF
05-30 13:08:55.167 6116 6180 I ReactNativeJS: In Splashscreen DidMount
05-30 13:08:55.308 6116 6180 I ReactNativeJS: 'SplashScreen token result is : ', ''
05-30 13:08:55.309 6116 6180 I ReactNativeJS: Splashscreen result is nothing
05-30 13:08:55.366 6116 6180 I ReactNativeJS: 'After retrieve in spliashscreen : ', { user: null, result: null, success: true }
我想知道每种颜色(红色,蓝色和绿色)出现“ b”,“ a”序列的次数(即,序列中b在a之前的次数)。
正确的答案如下:
x <- data.frame("Red" = c("a", "b", "a", "a", "c", "d"), "Blue" = c("b", "a", "b", "a", "b", "a"), "Green" = c("a", "a", "b", "a", "b", "a"))
答案 0 :(得分:1)
这是使用stringr
library(stringr)
count_pair <- function(x, pattern) {
p <- paste(pattern, collapse = "")
s <- paste(x, collapse = "")
str_count(s, pattern = p)
}
z <- apply(x, 2, count_pair, pattern = c("b", "a"))
# Red Blue Green
# 1 3 2
# if you want the output in form of a data.frame you could run:
df <- as.data.frame(as.table(z))
# Var1 Freq
# 1 Red 1
# 2 Blue 3
# 3 Green 2