use*_*187 3 string r 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]+", perl=TRUE)
# expected output ( I have tried the following)
df$maxvalue<- max(df$value)
df$maxvalue
8
4
0
Run Code Online (Sandbox Code Playgroud)
这应该可以解决问题
df$value <- lapply(lapply(strsplit(as.character(df$x),"[^1]+"), nchar),max)
Run Code Online (Sandbox Code Playgroud)
输出:
> df
x y value
1 000010011100011111001111111100 1 8
2 011110 2 4
3 0000000 3 0
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
151 次 |
| 最近记录: |