我正在尝试使用构造数组
> byrow=TRUE
Run Code Online (Sandbox Code Playgroud)
代替
> array()
Run Code Online (Sandbox Code Playgroud)
但是这个功能不可用。
例如:我输入
y<-array(1:24,c(4,3,2))
y
Run Code Online (Sandbox Code Playgroud)
我得到
, , 1
[,1] [,2] [,3]
[1,] 1 5 9
[2,] 2 6 10
[3,] 3 7 11
[4,] 4 8 12
, , 2
[,1] [,2] [,3]
[1,] 13 17 21
[2,] 14 18 22
[3,] 15 19 23
[4,] 16 20 24
Run Code Online (Sandbox Code Playgroud)
但我希望数字按行排列。所以我试过了
y<-array(1:24,c(4,3,2),byrow=TRUE)
Run Code Online (Sandbox Code Playgroud)
但我得到了
数组中的错误 (1:24, c(4, 3, 2), byrow = TRUE):未使用的参数 (byrow = TRUE)
我怎样才能达到我想要的?
另外,如何按其他维度排列数字?
我想将tibble具有列表列的磁盘保存到磁盘(仅供以后在R中使用)。理想情况下,我想要一种快速的二进制格式,例如feather,但是它似乎不支持list cols:
test <- tibble(a= list(c(1,2), c(3,4)))
feather::write_feather(test, 'test.csv')
Run Code Online (Sandbox Code Playgroud)
writeFeather(x,path)中的错误:未实现:a是一个列表
我期望readr程序包中的方法能够处理此问题,但是我尝试过的方法似乎都无法解决。
我该怎么做呢?
我想列出组内的唯一ID,用户可以在其中选择分组变量。以下作品:
if(useGroupVar1){
dt[,unique(id),.(group1a,group1b,group1c)]
} else {
dt[,unique(id),group2]
}
Run Code Online (Sandbox Code Playgroud)
我在代码中用于过滤行的表达式实际上很长,因此我想避免重复代码。我想出了这个“解决方案”,该解决方案实际上不起作用:
dt[,unique(id),if(useGroupVar1){.(group1a,group1b,group1c)}else{group2}]
Run Code Online (Sandbox Code Playgroud)
如果条件导致group2单独使用,则该方法有效(尽管该列称为if),但尝试使其在.(group1a,group1b,group1c)结果中使用
eval(expr,envir,enclos)中的错误:找不到函数“。”
现在,我读到的.()是的别名list(),因此使用后者可以得到
bysubl [[jj + 1L]]错误:下标超出范围
有没有一种方法可以实现条件by而不复制整个表达式?
我有这段代码,虽然它有效 - 在我的data.frame中处理(7分钟)530,000条记录需要相当长的时间.
我的目标是在我的框架中创建一个字段,并根据people $ Month的值填充它,如下所示:
for (i in 1:nrow(people)) {
if(people$Month[i]=='JAN') {
people[i, 'new_month'] <- "1"
}
else if(people$Month[i]=='FEB') {
people[i, 'new_month'] <- "2"
}
else if(people$Month[i]=='MAR') {
people[i, 'new_month'] <- "3"
}
else if(people$Month[i]=='APR') {
people[i, 'new_month'] <- "4"
}
else if(people$Month[i]=='MAY') {
people[i, 'new_month'] <- "5"
}
else if(people$Month[i]=='JUN') {
people[i, 'new_month'] <- "6"
}
else if(people$Month[i]=='JUL') {
people[i, 'new_month'] <- "7"
}
else if(people$Month[i]=='AUG') {
people[i, 'new_month'] <- "8"
}
else if(people$Month[i]=='SEP') {
people[i, 'new_month'] <- …Run Code Online (Sandbox Code Playgroud) 我正在尝试使用以下代码删除我创建的文件名中的空格:
epoch <- strsplit(toString(files[val]),split='.', fixed=TRUE)[[1]][1]
print(paste(epoch,".csv"))
Run Code Online (Sandbox Code Playgroud)
目前的输出给了我:"2016_Q3 .csv".我想删除和之间的空格3,.所以最后的字符串看起来像"2016_Q3.csv"
我已经看过gsub并trimws,但不能让他们的工作.
我有一个列表,告诉你一个人有多少可能的配偶.它看起来像这样:
列表:
$`A1`
[1] "D2" "E2" "F2" "H2"
$`B1`
[1] "G2" "I2" "J2" "K2" "L2"
$`C1`
[1] "J2" "M2" "N2" "O2" "P2"
[6] "Q2" "R2" "S2"
Run Code Online (Sandbox Code Playgroud)
因此,例如,个体A1的可能配偶是个体D2,个体E2,个体F2和个体H2.
我想把它变成一个数据框架,将个人与其可能的配对配对.所以我想要这样的东西:
DF:
Female ID Mate ID
A1 D2
A1 E2
A1 F2
A1 H2
B1 G2
B1 I2
B1 J2
B1 K2
B1 L2
C1 J2
C1 M2
C1 N2
C1 O2
C1 P2
C1 Q2
C1 R2
C1 S2
Run Code Online (Sandbox Code Playgroud) 这似乎很明显,但我无法弄清楚。我有一个包含州名和其他随机单词的字符向量,并想提取州名。
df <- data.frame(string = c("The quick brown Arizona","jumps over the Alabama","dog Arkansas"))
Run Code Online (Sandbox Code Playgroud)
我可以单独创建提取状态名称:
df$state[grepl("Alabama",df$string)] <- "Alabama"
Run Code Online (Sandbox Code Playgroud)
但我无法弄清楚如何在不复制和粘贴 42 次的情况下为所有状态复制它。我得到的最接近的是:
find.state <- function(x){
df$state[grepl(x,df$string)] <- x
}
lapply(state.name, find.state)
Run Code Online (Sandbox Code Playgroud)
但这只是打印所有州名。
这是一个玩具示例:
df <- data.frame(user=c('a','b'), rating=c(1,2), age=c(17,33))
rating <- function(df, var){x <- df %>% summarise(sum(var))}
rating(df,age)
Run Code Online (Sandbox Code Playgroud)
当我执行该函数时,我收到以下错误:
summarise_impl(.data,dots)出错:找不到对象'age'
如何将列名作为参数传递给函数?