鉴于三个(或n列表):
one <- list(a=1:2,b="one")
two <- list(a=2:3,b="two")
three <- list(a=3:4,b="three")
Run Code Online (Sandbox Code Playgroud)
为了得到这个结果,cbind在n列表中列出每个列表项的更有效方法是什么?
mapply(cbind,mapply(cbind,one,two,SIMPLIFY=FALSE),three,SIMPLIFY=FALSE)
$a
[,1] [,2] [,3]
[1,] 1 2 3
[2,] 2 3 4
$b
[,1] [,2] [,3]
[1,] "one" "two" "three"
Run Code Online (Sandbox Code Playgroud)
当这工作好n是2或3,但很快将成为可笑复杂.这有更有效的变化吗?我在SO上看过类似的问题,但一直在努力调整它们.
我有超过3年的数据.每年我都希望找到与当年的Jaunary 1相对应的那一天.例如:
> x <- c('5/5/2007','12/31/2007','1/2/2008')
> #Convert to day of year (julian date) –
> strptime(x,"%m/%d/%Y")$yday+1
[1] 125 365 2
Run Code Online (Sandbox Code Playgroud)
我想知道如何做同样的事情,但增加时间.但我仍然没有时间.谁能建议用日期和时间找到朱利安日期的更好方法是什么?
> x1 <- c('5/5/2007 02:00','12/31/2007 05:58','1/2/2008 16:25')
> #Convert to day of year (julian date) –
> strptime(x1,"%m/%d/%Y %H:%M")$yday+1
[1] 125 365 2
Run Code Online (Sandbox Code Playgroud)
而不是这个结果,我想要十进制天的输出.例如,第一个例子是125.0833333,依此类推.
非常感谢.
我的猜测是使用ddply很容易,但我仍然是R的新手,无法理解它.
我有一个看起来像这样的data.frame
txt <- "label var1 var2 var3 var4 var5 var6 var7
lab1 401 80 57 125 118 182 83
lab2 72 192 80 224 182 187 178
lab3 7 152 134 104 105 80 130
lab4 3 58 210 30 78 33 87
lab5 1 2 3 1 1 2 6"
mydata <- read.table(textConnection(txt), sep = " ", header = TRUE)
Run Code Online (Sandbox Code Playgroud)
这样做我可以一次将一个变量转换为百分比
mydata$var1 <- round(prop.table(mydata$var1),3)*100
Run Code Online (Sandbox Code Playgroud)
但是如何在一个笔划中的data.frame中使用所有变量(var1:var7)呢?
注意:它进入一个函数,其中变量的长度和数量不时变化,因此代码应该对此敏感.
先感谢您
所以我的R输出看起来像这样
Time 50 100 150 200 250 300 350
Carla 1.2 1.8 2.2 2.3 3.0 2.5 1.8
Mace 1.5 1.1 1.9 2.0 3.6 3.0 2.5
Lea 1.7 1.6 2.3 2.7 2.6 2.2 2.6
Karen 1.3 1.7 1.9 2.2 3.2 1.5 1.9
Run Code Online (Sandbox Code Playgroud)
我希望它看起来像这样
Time Score Name
50 1.2 Carla
50 1.5 Mace
50 1.7 Lea
50 1.3 Karen
100 1.8 Carla
100 1.5 Mace
100 1.7 Lea
100 1.3 Karen
Run Code Online (Sandbox Code Playgroud)
我该如何将其转换为此?谢谢
I am having a "cannot open the connection" error in an R script. Below are the details:
> write.csv(table1.fiveReitWts,file="C:\\Users\\John Broussard\\Dropbox\\evtHandbookProject\\figuresTables\\figure3Data.csv")
Error in file(file, ifelse(append, "a", "w")) :
cannot open the connection
In addition: Warning message:
In file(file, ifelse(append, "a", "w")) :
cannot open file 'C:\Users\John Broussard\Dropbox\evtHandbookProject\figuresTables\figure3Data.csv': No such file or directory
>
Run Code Online (Sandbox Code Playgroud)
This is a new file being created, but the directory exists. I can see the directory, which contains other files. I have used this code previously with success, …
我有一个df,我想删除df中行数少于X的人.例如,在这个玩具示例中,我想留下那些> = 5行的人.
df
names fruit
4 john kiwi
7 john apple
9 john banana
13 john orange
14 john apple
2 mary orange
5 mary apple
8 mary orange
10 mary apple
12 mary apple
1 tom apple
3 tom banana
6 tom apple
11 tom kiwi
Run Code Online (Sandbox Code Playgroud)
示例输出
df
names fruit
4 john kiwi
7 john apple
9 john banana
13 john orange
14 john apple
2 mary orange
5 mary apple
8 mary orange
10 mary apple
12 …Run Code Online (Sandbox Code Playgroud) 我想为我自己的功能创建帮助页面。所以如果我定义一个函数:例如:
myfunct <- function(x,y,z){...}
Run Code Online (Sandbox Code Playgroud)
如果我输入,?myfunct我会得到我给出的参数规格。
有没有办法做到这一点?
我是编程的新手,我才刚刚开始学习R语言。我正在尝试进行冒泡排序,但是显示以下错误消息。谁能帮我解决问题?
x <-sample(1:100,10)
n <- length(x)
example <- function(x)
{
for (i in 1:n-1)
{
while (x[i] > x[i+1])
{
temp <- x[i+1]
x[i+1] <- x[i]
x[i] <- temp
}
i <- i+1
}
}
example(x)
Run Code Online (Sandbox Code Playgroud)
while(x [i]> x [i + 1])中的错误{{参数长度为零
我有一个简单的列表和一个相同长度的简单向量.我想将ith向量的ith元素添加到列表的元素中.有没有办法比这个循环更好?
test <- list(element1=list(a=1,b=2,a1=8),
element2=list(a=9,d=17))
vec <- c(12,25)
for (i in 1:length(test)){
test[[i]] <- c(test[[i]],vec[i])
}
Run Code Online (Sandbox Code Playgroud) 我有以下数据作为示例:
basketball = c("MISS W. Johnson 18' Pullup Jump Shot",
"MISS Barnes 12' Pullup Jump Shot",
"MISS Carter 19' Pullup Jump Shot")
Run Code Online (Sandbox Code Playgroud)
我如何找到最常见的单词或"相交"它们,以便我只能得到"MISS Pullup Jump Shot"结果?