我有一个data.table对象,其中包含多个指定唯一案例的列.在下面的小示例中,变量" name"," job"和" sex"指定唯一ID.我想添加缺失的行,以便每个case都有一行用于另一个变量的每个可能实例," from"(类似于expand.grid).
library(data.table)
set.seed(1)
mydata <- data.table(name = c("john","john","john","john","mary","chris","chris","chris"),
job = c("teacher","teacher","teacher","teacher","police","lawyer","lawyer","doctor"),
sex = c("male","male","male","male","female","female","male","male"),
from = c("NYT","USAT","BG","TIME","USAT","BG","NYT","NYT"),
score = rnorm(8))
setkeyv(mydata, cols=c("name","job","sex"))
mydata[CJ(unique(name, job, sex), unique(from))]
Run Code Online (Sandbox Code Playgroud)
这是当前的data.table对象:
> mydata
name job sex from score
1: john teacher male NYT -0.6264538
2: john teacher male USAT 0.1836433
3: john teacher male BG -0.8356286
4: john teacher male TIME 1.5952808
5: mary police female USAT 0.3295078
6: chris lawyer female …Run Code Online (Sandbox Code Playgroud) 我的实际数据看起来像
8/8/2013 15:10
7/26/2013 10:30
7/11/2013 14:20
3/28/2013 16:15
3/18/2013 15:50
Run Code Online (Sandbox Code Playgroud)
当我从excel文件中读取时,R读为,
41494.63
41481.44
41466.60
41361.68
41351.66
Run Code Online (Sandbox Code Playgroud)
所以我用了,as.POSIXct(as.numeric(x[1:5])*86400, origin="1899-12-30",tz="GMT")我得到了,
2013-08-08 15:07:12 GMT
2013-07-26 10:33:36 GMT
2013-07-11 14:24:00 GMT
2013-03-28 16:19:12 GMT
2013-03-18 15:50:24 GMT
Run Code Online (Sandbox Code Playgroud)
为什么时间有差异?如何克服它?
我有两个具有不同列数和行数的数据框。我想将它们组合成一个数据框。
> month.saf
Name NCDC Year Month Day HrMn Temp Q
244 AP 99999 2014 2 1 0 12 1
245 AP 99999 2014 2 1 300 12.2 1
246 AP 99999 2014 2 1 600 14.4 1
247 AP 99999 2014 2 1 900 18.6 1
248 AP 99999 2014 2 1 1200 18 1
249 AP 99999 2014 2 1 1500 13.6 1
250 AP 99999 2014 2 1 1800 11.8 1
251 AP 99999 2014 2 1 …Run Code Online (Sandbox Code Playgroud) 例如,假设我希望能够定义一个函数,该函数返回与第一个参数连接的赋值变量的名称:
a <- add_str("b")
a
# "ab"
Run Code Online (Sandbox Code Playgroud)
上面示例中的函数看起来像这样:
add_str <- function(x) {
arg0 <- as.list(match.call())[[1]]
return(paste0(arg0, x))
}
Run Code Online (Sandbox Code Playgroud)
但是,函数的arg0行被一行替换,该行将获得被赋值变量的名称("a")而不是函数的名称.
我已经尝试过使用match.call和sys.call,但我无法让它工作.这里的想法是在变量和函数结果上调用赋值运算符,因此应该是函数调用的父调用.
我有一个重复序列的数据集TRUE,我希望根据某些条件标记 - 依据id序列的增量值.A FALSE打破TRUEs 的序列,并且第一个FALSE打破任何给定序列的序列TRUE应包括在该序列中.FALSEs之间TRUE的连续s 是无关紧要的,标记为0.
例如:
> test
id logical sequence
1 1 TRUE 1
2 1 TRUE 1
3 1 FALSE 1
4 1 TRUE 2
5 1 TRUE 2
6 1 FALSE 2
7 1 TRUE 3
8 2 TRUE 1
9 2 TRUE 1
10 2 TRUE 1
11 2 FALSE 1
12 2 TRUE 2
13 2 TRUE 2
14 …Run Code Online (Sandbox Code Playgroud) 我有两列数据,X并Y具有在两个向量到小数点后4位数据的每一个条目.
当我用plot(x,y)轴进行简单绘图时,数据显示最多2位小数精度.如何在两个轴上将其更改为4小数点精度?
我添加了下面(inputData)的示例数据,我已经使用了plot(inputData).
inputData=structure(list(X = c(33.73521973, 33.622022, 33.63591706, 33.58184488,
33.73027696, 33.76169838), Y = c(-112.2559051, -112.2396135,
-112.2345327, -112.2441752, -112.2463008, -112.3298128)), .Names = c("X",
"Y"), row.names = c(NA, 6L), class = "data.frame")
Run Code Online (Sandbox Code Playgroud)
我想在上面的数据集中找到一个可重现的例子,以及建议答案的一部分.
> inputData
X Y
1 33.73522 -112.2559
2 33.62202 -112.2396
3 33.63592 -112.2345
4 33.58184 -112.2442
5 33.73028 -112.2463
6 33.76170 -112.3298
Run Code Online (Sandbox Code Playgroud)

如何删除R中的连续重复条目?我认为with可能会被使用,但无法思考如何使用它.举例说明:
read.table(text = "
a t1
b t2
b t3
b t4
c t5
c t6
b t7
d t8")
Run Code Online (Sandbox Code Playgroud)
样本数据:D
events time
a t1
b t2
b t3
b t4
c t5
c t6
b t7
d t8
Run Code Online (Sandbox Code Playgroud)
要求结果:
events time
a t1
b t4
c t6
b t7
d t8
Run Code Online (Sandbox Code Playgroud)
`
我有点难以将极坐标图中的一条线的端点与起点连接起来。
我的数据:
df <- structure(list(ri = c(0, 10, 20, 30, 40, 50, 60, 70, 80, 90, 100, 110, 120, 130, 140, 150, 160, 170, 180, 190, 200, 210, 220, 230, 240, 250, 260, 270, 280, 290, 300, 310, 320, 330, 340, 350, 360),
n = c(329L, 315L, 399L, 700L, 919L, 757L, 656L, 918L, 1117L, 976L, 878L, 803L, 811L, 1072L, 1455L, 1642L, 1891L, 1688L, 1553L, 1841L, 2061L, 2321L, 2498L, 2080L, 1595L, 1080L, 1002L, 953L, 729L, 604L, 538L, 489L, 535L, 455L, 328L, …Run Code Online (Sandbox Code Playgroud) 我正在使用将 csv 加载到数据框中
str <- readLines("Messages.csv", n=-1, skipNul=TRUE)
matches <- str_match(str, pattern = "\\s*([0-9]{2}/[0-9]{2}/[0-9]{4}),\\s*([0-9]{2}:[0-9]{2}:[0-9]{2}),\\s*(Me|Them),\\s*(\\+[0-9]{11,12}),\\s*((?s).*)")
df <- data.frame(matches[, -1], stringsAsFactors=F)
colnames(df) <- c("date","time","sender","phone number","msg")
# Format the date and create a row with the number of characters of the messages
df <- df %>%
mutate(posix.date=parse_date_time(paste0(date,time),"%d%m%y%H%M%S"),tz="Europe/London") %>%
mutate(nb.char = nchar(msg)) %>%
select(posix.date, sender, msg, nb.char) %>%
arrange(as.numeric(posix.date))
Run Code Online (Sandbox Code Playgroud)
我可以使用更改发件人姓名
# Change the senders' names
df <- df %>%
mutate(sender = replace(sender, sender == "Me", "Mr. Awesome"))
Run Code Online (Sandbox Code Playgroud)
但我想将数据的时区从 tz="America/Los_Angeles" 更改为
我已尝试以下两种方法均未成功:
attributes(df)$tz<-"America/Los_Angeles"
Run Code Online (Sandbox Code Playgroud)
这可以编译,但似乎没有任何改变
还有这个: …
我有固定宽度的数据文件(.dbf),没有行分隔符.以下是该数据文件的两行:
20141101 77h 3.210 0 3 20141102 76h 3.090 0 3
Run Code Online (Sandbox Code Playgroud)
一行的宽度c(8,4,7,41)用于日期(8),一些时间度量(4),数据点(7)以及我可以在一个"休息"列(41)中汇总的一些其他列.在一行之后没有分隔符,下一行只是附加到第一行.所有时间步骤基本上都是连续写入一条大线.此文件中只包含数字,字符和空格.
与read.fwf('filepath', widths = c(8,4,7,41))第一行之后的R停止读取,由于缺乏在线分离器的.
read.fwf()当没有行分隔符时,是否有一个参数告诉您何时开始读取新行?或者我应该使用不同的读命令?
提前致谢.