Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.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_String_List_Dataframe_Stringr - Fatal编程技术网

如何在R中获取数据帧中列表的最大值

如何在R中获取数据帧中列表的最大值,r,string,list,dataframe,stringr,R,String,List,Dataframe,Stringr,我正在尝试创建一个新列,以获取数据帧中列表的最大值。我想知道如何从df$value列创建这个名为maxvalue的列,也就是说,我想得到列中该列表的最大值 x <- c( "000010011100011111001111111100", "011110", "0000000") y<- c(1, 2,3) df<- data.frame(x,y) library(stringr) df$value <- strsplit(df$x, "[^1]+", pe

我正在尝试创建一个新列,以获取数据帧中列表的最大值。我想知道如何从df$value列创建这个名为maxvalue的列,也就是说,我想得到列中该列表的最大值

  x <- c( "000010011100011111001111111100", "011110", "0000000")
  y<- c(1, 2,3)
 df<- data.frame(x,y)
 library(stringr)
 df$value <- strsplit(df$x, "[^1]+", perl=TRUE)
  # expected output  ( I have tried the following)
 df$maxvalue<- max(df$value) 
  df$maxvalue
   8 
   4
   0

x这应该可以做到

df$value <- lapply(lapply(strsplit(as.character(df$x),"[^1]+"), nchar),max)

@Daniel O逻辑的简化版本:

df$value <- sapply(strsplit(as.character(df$x),"[^1]+"), function(x){max(nchar(x))})

df$value我们也可以使用
rawToChar
charToRaw

sapply(as.character(df$x), function(x) 
      with(rle(charToRaw(x)), max(lengths[as.character(values) == 31])))

是字符串中1的输出和吗?此外,请澄清max值,你是否将它与y字段进行比较以找出最大值?它是1系列的连续序列的最大值,例如10001100111,在这种情况下,它将是3,因为我的答案的最大连续性帮助了你,请考虑接受它。你可以稍微简化一些。在本例中,您的函数不需要
{
}
case@DanielO虽然您是正确的,但这实际上不会对代码的输入/输出或速度产生任何影响。
sapply(as.character(df$x), function(x) 
      with(rle(charToRaw(x)), max(lengths[as.character(values) == 31])))