目标是从下面数据的Code列创建两个新列。一个是数字,另一个是代码(因子)。我怎么做?我试过了ifelse()
,但它给了错误。
structure(list(Potreiro = structure(c(3L, 3L, 3L, 3L,
3L, 4L, 3L, 4L, 3L, 4L), .Label = c("1A", "6B", "7A", "7B"), class =
"factor"), Code = structure(c(4L, 1L, 8L, 3L, 2L, 4L, 6L, 5L, 8L, 7L ),
.Label = c("2", "3", "4", "5", "50%", "70%", "ac", "ad", "av", "cd", "de",
"Dem"), class = "factor")), .Names = c("Potreiro", "Code"), row.names =
c(NA, 10L), class = "data.frame")
感谢!!!
答案 0 :(得分:2)
Timer timer = new Timer();
timer.schedule(new TimerTask() {
@Override
public void run() {
getActivity().runOnUiThread(runnable);
}
}, 0, 5000);
library(dplyr)
library(stringr)
df <- structure(list(Potreiro = structure(c(3L, 3L, 3L, 3L, 3L, 4L, 3L, 4L, 3L, 4L),
.Label = c("1A", "6B", "7A", "7B"),
class = "factor"),
Code = structure(c(4L, 1L, 8L, 3L, 2L, 4L, 6L, 5L, 8L, 7L ),
.Label = c("2", "3", "4", "5", "50%", "70%", "ac", "ad", "av", "cd", "de", "Dem"),
class = "factor")),
.Names = c("Potreiro", "Code"),
row.names = c(NA, 10L),
class = "data.frame")
df %>%
mutate(
number = str_extract_all(Code, "\\d+"),
word = str_extract(Code, "\\D[^%]")
)
变量正则表达式正在查找数字,并且至少会匹配一次number
。
\\d+
变量正则表达式在剥离%符号时不会查找数字。
结果:
word
答案 1 :(得分:1)
我会这样做:
df <-
structure(list(Potreiro = structure(c(3L, 3L, 3L, 3L,
3L, 4L, 3L, 4L, 3L, 4L), .Label = c("1A", "6B", "7A", "7B"), class =
"factor"), Code = structure(c(4L, 1L, 8L, 3L, 2L, 4L, 6L, 5L, 8L, 7L ),
.Label = c("2", "3", "4", "5", "50%", "70%", "ac", "ad", "av", "cd", "de",
"Dem"), class = "factor")), .Names = c("Potreiro", "Code"), row.names =
c(NA, 10L), class = "data.frame")
arenum <- sapply(df$Code, function (x) grepl('[[:digit:]]', x))
df$codenum <- ifelse(arenum, as.character(df$Code), NaN)
df$codechar <- ifelse(!arenum, as.character(df$Code), NaN)
df
如果你真的不想要任何其他东西而不是数字改变arnum:
arenum <- sapply(df$Code, function (x) gsub('[[:digit:]]', '', x) == '')
答案 2 :(得分:0)
以下是使用extract
library(dplyr)
library(tidyr)
df %>%
extract(Code, into = c('number', 'word'), '(\\d*)([a-z]*)', remove = FALSE, convert = TRUE)
# Potreiro Code number word
#1 7A 5 5
#2 7A 2 2
#3 7A ad NA ad
#4 7A 4 4
#5 7A 3 3
#6 7B 5 5
#7 7A 70% 70
#8 7B 50% 50
#9 7A ad NA ad
#10 7B ac NA ac