Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/65.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R 使用外部表计算字符串长度_R_Tidyverse - Fatal编程技术网

R 使用外部表计算字符串长度

R 使用外部表计算字符串长度,r,tidyverse,R,Tidyverse,假设您有一个数据表: df<-tibble(person = c("Alice", "Bob", "Mary"), colour = c("Red", "Green", "Blue"), city = c("London", "Paris", "New York"))

假设您有一个数据表:

df<-tibble(person = c("Alice", "Bob", "Mary"),
           colour = c("Red", "Green", "Blue"),
           city = c("London", "Paris", "New York"))

# A tibble: 3 x 3
  person colour city    
  <chr>  <chr>  <chr>   
1 Alice  Red    London  
2 Bob    Green  Paris   
3 Mary   Blue   New York

df将基R与
mapply一起使用

df[rowSums(mapply(function(x, y) nchar(x) > y, df, len$field_length)) > 0, ]

# A tibble: 2 x 3
#  person colour city    
#  <chr>  <chr>  <chr>   
#1 Bob    Green  Paris   
#2 Mary   Blue   New York

带有
c\u交叉()的
dplyr
解决方案

库(dplyr)
df%>%
行()
过滤器(任意(nchar(跨越(所有内容())>len$field\u长度))%>%
解组()
##tibble:2 x 3
#人色城市
#          
#1鲍勃格林巴黎酒店
#2玛丽蓝纽约

根据两个矩阵更容易解决问题,首先是每个条目的长度:

nchar(as.matrix(df))
     person colour city
[1,]      5      3    6
[2,]      3      5    5
[3,]      4      4    8
以及允许长度的对应矩阵:

allowed = replicate(nrow(df),len$field_length[match(colnames(df),len$field_name)])

allowed

     [,1] [,2] [,3]
[1,]   12   12   12
[2,]    4    4    4
[3,]    6    6    6
然后按矩阵进行比较,只保留行和()为

df[rowMeans(nchar(as.matrix(df))>允许)>0,]
#一个tibble:2x3
人色城市
1鲍勃格林巴黎酒店
2玛丽蓝纽约
如果您的两个data.frames的顺序与您的示例相同,您可以(感谢@zx8754)指出:

df[rowMeans(nchar(as.matrix(df)) > len$field_length)>0,]
# A tibble: 2 x 3
  person colour city    
  <chr>  <chr>  <chr>   
1 Bob    Green  Paris   
2 Mary   Blue   New York
df[rowMeans(nchar(as.matrix(df))>len$field_length)>0,]
#一个tibble:2x3
人色城市
1鲍勃格林巴黎酒店
2玛丽蓝纽约

df
转换为与
len
相同的格式,并将两者合并。在此之后,将每个字符串与
字段的长度进行比较就很简单了

library(tidyverse)

test_result_df <- df %>% 
  mutate(id = row_number()) %>% 
  pivot_longer(-id, names_to = 'field_name') %>% 
  left_join(len, by = 'field_name') %>% 
  mutate(test_passed = str_length(value) <= field_length) %>% 
  group_by(id) %>% 
  summarise(all_passed = all(test_passed))

df[!test_result_df$all_passed,]

库(tidyverse)
测试结果\u df%
变异(id=行号())%>%
pivot_longer(-id,names_to='field_name')%>%
左联接(len,by='field\u name')%>%
突变(测试通过=str长度(值)%
分组依据(id)%>%
总结(全部通过=全部(测试通过))
df[!测试结果df$全部通过,]
#一个tible:2x3
人色城市
1鲍勃格林巴黎酒店
2玛丽蓝纽约

感谢Ronak再次救了我!我与自己的
tidyverse
解决方案(未发布)非常接近通过沿着
pivot\uuu
路线走,但在这里发布之前没有得到完整的解决方案。这看起来是一个非常好的解决方案。我以前没有遇到过
c\u
。接受这个答案,因为它避免了
左入
。我没有考虑过这个方法,谢谢你发布。我会把这个放在我的文章后面注意未来的问题。有趣的方法,轻微的改进,不需要允许,尝试:
nchar(as.matrix(df))>len$field\u length
True,如果已经是相同的顺序。我可以补充。好提示。Lol有时我对复制向量感到非常不安。
allowed = replicate(nrow(df),len$field_length[match(colnames(df),len$field_name)])

allowed

     [,1] [,2] [,3]
[1,]   12   12   12
[2,]    4    4    4
[3,]    6    6    6
df[rowMeans(nchar(as.matrix(df)) > allowed)>0,]
# A tibble: 2 x 3
  person colour city    
  <chr>  <chr>  <chr>   
1 Bob    Green  Paris   
2 Mary   Blue   New York
df[rowMeans(nchar(as.matrix(df)) > len$field_length)>0,]
# A tibble: 2 x 3
  person colour city    
  <chr>  <chr>  <chr>   
1 Bob    Green  Paris   
2 Mary   Blue   New York
library(tidyverse)

test_result_df <- df %>% 
  mutate(id = row_number()) %>% 
  pivot_longer(-id, names_to = 'field_name') %>% 
  left_join(len, by = 'field_name') %>% 
  mutate(test_passed = str_length(value) <= field_length) %>% 
  group_by(id) %>% 
  summarise(all_passed = all(test_passed))

df[!test_result_df$all_passed,]

# A tibble: 2 x 3
  person colour city    
  <chr>  <chr>  <chr>   
1 Bob    Green  Paris   
2 Mary   Blue   New York