我想在dplyr的mutate中使用switch语句。我有一个简单的函数,可以执行一些操作并通过switch分配备用值,例如:
convert_am <- function(x) {
x <- as.character(x)
switch(x,
"0" = FALSE,
"1" = TRUE,
NA)
}
Run Code Online (Sandbox Code Playgroud)
当应用于标量时,这可以按需要工作:
>> convert_am(1)
[1] TRUE
>> convert_am(2)
[1] NA
>> convert_am(0)
[1] FALSE
Run Code Online (Sandbox Code Playgroud)
我想通过mutate致电获得相同的结果:
mtcars %>% mutate(am = convert_am(am))
Run Code Online (Sandbox Code Playgroud)
这将失败:
错误
mutate_impl(.data, dots):评估错误:EXPR必须是长度为1的向量。
我了解这是因为传递给switch ar的值不是单一的,例如:
convert_am(c(1,2,2))错误switch(x, 0 = FALSE, 1 = TRUE, NA):EXPR必须是长度为1的向量
尝试向量化也会产生所需的结果:
convert_am <- function(x) {
x <- as.character(x)
fun_switch <- function(x) {
switch(x,
"0" = FALSE,
"1" = TRUE,
NA)
}
vf <- …Run Code Online (Sandbox Code Playgroud) 我正在尝试利用 TOpenDialog 将所选文件的路径传递给 AdoConection 并将 Excel 文件的内容加载到表中。我当前正在尝试下面的代码,但代码的最后一部分未连接到 Excel,返回错误: [dcc32 Error] sample_map.pas(80): E2010 Incompile types: 'string' and 'TOpenDialog'
procedure TForm1.Button1Click(Sender: TObject);
var
openDialog : TOpenDialog; // Open dialog variable
strConn : WideString; // Declare wide string for the connection
begin
// Create the open dialog object - assign to our open dialog variable
openDialog := TOpenDialog.Create(self);
// Set up the starting directory to be the current one
openDialog.InitialDir := GetCurrentDir;
// Only allow existing files to be selected
openDialog.Options := [ofFileMustExist]; …Run Code Online (Sandbox Code Playgroud) 我正在使用类似于以下摘录的数据框.
sample.df
Obs Var1 Var2 Var3
A0001 21 21 21
A0002 21 78 321
A0003 32 98 87
A0004 21 12 54
A0005 21 13 45
A0006 21 87 45
B0007 84 NA 45
B0008 21 NA 98
B0009 2 NA 45
B0010 12 NA 45
Run Code Online (Sandbox Code Playgroud)
我想删除缺少某些变量百分比的列,例如80%.我试过下面的代码:
sample.df.cln <- apply(sample.df, 2, function(x) {
if (sum(is.na(x)) / nrow(x) > 0.8) {
x <- NULL
}
})
Run Code Online (Sandbox Code Playgroud)
但它返回了以下错误:
Error in if (sum(is.na(x))/nrow(x) > 0.8) { : argument is of length zero
Run Code Online (Sandbox Code Playgroud)
我将不胜感激任何帮助.我还认为将代码封装在函数中是有益的,因此它可以应用于不同的数据帧.
我感兴趣的是使用的特殊的呼叫中call/ eval在代码:
eval(call("mean", c(2,3)))
Run Code Online (Sandbox Code Playgroud)
这将正确地产生结果2.5.现在,我想在特殊调用中使用相同的语法.
+呼叫:
eval(call("`+`", c(2,3)))
Run Code Online (Sandbox Code Playgroud)
产生错误:
eval中的错误(expr,envir,enclos):找不到函数"
+"
与通话类似,
eval(call("+", c(2,3)))
Run Code Online (Sandbox Code Playgroud)
没有产生预期的结果:
[1] 2 3
Run Code Online (Sandbox Code Playgroud)期望的结果应该简单地返回长度为1的向量,其中单值为5,如通过2 + 3调用获得的.
我有兴趣计算传递给函数的参数数量。 length不能用于此目的:
>> length(2,2,2,2,2)
Error in length(2, 2, 2, 2, 2) :
5 arguments passed to 'length' which requires 1
Run Code Online (Sandbox Code Playgroud)
这很明显,因为length有1个参数,所以:
length(c(2,2,2,2,2))
Run Code Online (Sandbox Code Playgroud)
会产生预期的结果-5。
我想这样调用我的函数myFunction(arg1, arg2, arg3)。这可以通过使用省略号来完成:
myCount <- function(...) {length(list(...))}
Run Code Online (Sandbox Code Playgroud)
myCount 会产生预期的结果:
>> myCount(2,2,2,2,2)
[1] 5
Run Code Online (Sandbox Code Playgroud)
这是非常低效的。我在大量参数上调用此函数,创建仅用于计数对象数量的列表是浪费的。返回传递给函数的参数数量的更好方法是什么?
我想利用这个na.locf来保存数据帧的非缺失值,其中第一次观察可能为零.
dta <- data.frame(A = c(NA, NA, 1, 2, 4, 5, NA, NA, NA),
B = c(NA, 5, 4, 5, 8, 9, NA, NA, 100))
dta %>% mutate_all(.funs = funs(na.locf(.)))
Run Code Online (Sandbox Code Playgroud)
错误
mutate_impl(.data, dots):列A必须是长度9(行数)或1,而不是7
Vectorize(require)(package = c("dplyr", "zoo"),
character.only = TRUE)
dta <- data.frame(A = c(0, NA, 1, 2, 4, 5, NA, NA, NA),
B = c(0, 5, 4, 5, 8, 9, NA, NA, 100))
dta %>% mutate_all(.funs = funs(na.locf(.)))
Run Code Online (Sandbox Code Playgroud)
潜在的解决方法可能涉及用 …
给定样本字符串:
tst_str <- c("abc", "123", "klm", "lop")
Run Code Online (Sandbox Code Playgroud)
我想做以下替换:
abc -> za12123 -> poiklm -> uyt简单的嵌套嵌套gsub可以产生以下结果:
gsub(
pattern = "abc",
replacement = "za12",
x = gsub(
pattern = "123",
replacement = "poi",
x = gsub(
pattern = "klm",
replacement = "uyt",
x = tst_str
)
)
)
# [1] "za12" "poi" "uyt" "lop"
Run Code Online (Sandbox Code Playgroud)
我想使用purrr::map*或purrr::reduce函数得出相同的结果.我最初的想法是利用purrr::reduce2
purrr::reduce2(
.x = c("abc", "123", "klm"),
.y = c("za12", "poi", "uyt"),
.f = function(x, y, …Run Code Online (Sandbox Code Playgroud) 我需要从一个data.table或tbl_df它的名字中获取一列的索引,
有没有比以下更简单的方法:
getColIndex <- function(df, colnameTarget = "myColumnName") {
colnames <- names(traindata_subset)
colIndex <- 1
found <- FALSE
for(colname in colnames) {
if (colname == colnameTarget) {
found <- TRUE
break
}
colIndex <- colIndex + 1
}
if (found) {
return (colIndex)
}
else {
return (-1)
}
}
Run Code Online (Sandbox Code Playgroud)
特别是使用哈德利的dplyr?
我有一个类似下面的列表:
# Initial object
vec <- c("levelA-1", "levelA-2", "levelA-3",
"levelB-1", "levelB-2", "levelB-3")
lstVec <- strsplit(x = vec, split = "-")
Run Code Online (Sandbox Code Playgroud)
我想到达以下结构列表:
lstRes <- list(levelA = list(1:3),
lvelB = list(1:3))
Run Code Online (Sandbox Code Playgroud)
该列表具有以下特征:
strsplit这些列表的元素创建的第二级元素