我有一个csv文件,当我使用此命令
SOLK<-read.table('Book1.csv',header=TRUE,sep=';')
Run Code Online (Sandbox Code Playgroud)
我得到了这个输出
> SOLK
Time Close Volume
1 10:27:03,6 0,99 1000
2 10:32:58,4 0,98 100
3 10:34:16,9 0,98 600
4 10:35:46,0 0,97 500
5 10:35:50,6 0,96 50
6 10:35:50,6 0,96 1000
7 10:36:10,3 0,95 40
8 10:36:10,3 0,95 100
9 10:36:10,4 0,95 500
10 10:36:10,4 0,95 100
. . . .
. . . .
. . . .
285 17:09:44,0 0,96 404
Run Code Online (Sandbox Code Playgroud)
该str(SOLK)
结果这
'data.frame': 285 obs. of 3 variables:
$ Time : Factor w/ 174 levels …
Run Code Online (Sandbox Code Playgroud) 数据框 AEbySOC 包含两列 - 具有字符级别的因子 SOC 和整数计数 Count:
> str(AEbySOC)
'data.frame': 19 obs. of 2 variables:
$ SOC : Factor w/ 19 levels "","Blood and lymphatic system disorders",..: 1 2 3 4 5 6 7 8 9 10 ...
$ Count: int 25 50 7 3 1 49 49 2 1 9 ...
Run Code Online (Sandbox Code Playgroud)
SOC 的级别之一是空字符串:
> l = levels(AEbySOC$SOC)
> l[1]
[1] ""
Run Code Online (Sandbox Code Playgroud)
我想用非空字符串替换此级别的值,例如“未指定”。这不起作用:
> library(plyr)
> revalue(AEbySOC$SOC, c(""="Not specified"))
Error: attempt to use zero-length variable name
Run Code Online (Sandbox Code Playgroud)
这也不行:
> AEbySOC$SOC[AEbySOC$SOC==""] …
Run Code Online (Sandbox Code Playgroud) 无法修复以下逻辑回归的以下错误
training=(IBM$Serial<625)
data=IBM[!training,]
dim(data)
stock.direction <- data$Direction
training_model=glm(stock.direction~data$lag2,data=data,family=binomial)
###Error### ---- Error in eval(family$initialize) : y values must be 0 <= y <= 1
Run Code Online (Sandbox Code Playgroud)
我正在使用的数据中的几行
X Date Open High Low Close Adj.Close Volume Return lag1 lag2 lag3 Direction Serial
1 28-11-2012 190.979996 192.039993 189.270004 191.979996 165.107727 3603600 0.004010855 0.004010855 -0.001198021 -0.006354834 Up 1
2 29-11-2012 192.75 192.899994 190.199997 191.529999 164.720734 4077900 0.00114865 0.00114865 -0.004020279 -0.009502386 Up 2
3 30-11-2012 191.75 192 189.5 190.070007 163.465073 4936400 0.003630178 0.003630178 -0.001894039 -0.005576956 Up 3 …
Run Code Online (Sandbox Code Playgroud) 如上.数据帧是一系列整数,它们是年龄.我试图将它们转换为序数变量.代码如下.
df <- read.table("http://dl.dropbox.com/u/822467/df.csv", header = TRUE, sep = ",")
df[(df >= 0) & (df <= 14)] <- "Age1"
df[(df >= 15) & (df <= 44)] <- "Age2"
df[(df >= 45) & (df <= 64)] <- "Age3"
df[(df > 64)] <- "Age4"
table(df)
Run Code Online (Sandbox Code Playgroud)
我们可以看到这不起作用.任何人都可以帮我提出一个更好的方法吗?
我正在重组一个物种名称的数据集.它有一个带有拉丁名称的列和具有普通名称的列(如果可用).我想制作第3列,在可用时给出琐碎的名称,否则为拉丁名称.琐碎的名字和拉丁名字都属于因子级.我试过if-loop:
if(art2$trivname==""){
art2$artname=trivname
}else{
art2$artname=latname
}
Run Code Online (Sandbox Code Playgroud)
它给了我正确的trivnames,但只在提供拉丁名字时给出了NA.
当我使用ifelse时,我只会得到数字.
一如既往,所有帮助赞赏:)
我无法在上传递此rle
功能data.frame
。函数在另一组上效果很好:
fgroup <- aggregate(fevents2[,3:14], list(weeks = fevents2[, 1]), function(x) rle(x)$values)
Run Code Online (Sandbox Code Playgroud)
产生错误:
Error in rle(x) : 'x' must be an atomic vector
Run Code Online (Sandbox Code Playgroud)
样本数据:
> dput(fevents2[1:20,])
structure(list(weeks = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = c("1",
"2", "3", "4", "5", "6", "7"), class = "factor"), A1M.Date = structure(c(1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, …
Run Code Online (Sandbox Code Playgroud) 我有一个相关矩阵,当我尝试使用“ factanal”函数对其进行最大似然因子分析时,出现以下错误:
factanal(covmat=matches_adjusted, factors=5, rotation=none, na.omit(matches_adjusted))
Warning in log(e) : NaNs produced
Error in optim(start, FAfn, FAgr, method = "L-BFGS-B", lower = lower, :
L-BFGS-B needs finite values of 'fn'
Run Code Online (Sandbox Code Playgroud)
是什么原因造成的?
我试图总结家庭调查的数据,因此我的大多数数据都是分类(因子)数据.我希望用某些问题的回答频率来总结它(例如,回答某些问题的家庭百分比的条形图,误差条显示置信区间).我发现这个优秀的教程我认为是我祷告的答案(http://www.cookbook-r.com/Manipulating_data/Summarizing_data/)但事实证明这只会对连续数据有所帮助.
我需要的是类似的东西,这将允许我计算这些比例的计数和标准误差/置信区间的比例.
基本上我希望能够为我的调查数据中提出的每个问题生成如下所示的汇总表:
# X5employf X5employff N(count) proportion SE of prop. ci of prop
# 1 1 20 0.64516129 ? ?
# 1 2 1 0.03225806 ? ?
# 1 3 9 0.29032258 ? ?
# 1 NA 1 0.290322581 ? ?
# 2 4 1 0.1 ? ?
structure(list(X5employf = structure(c(1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L), .Label = c("1", "2", "3"), class = "factor"), X5employff = structure(c(1L, 2L, 3L, NA, …
Run Code Online (Sandbox Code Playgroud) "epitools"包的一些示例存在一些问题,例如具有该epicurve.dates
功能.
这是一个流行曲线的简单例子(按天计算epicurve.dates
)
sampdates <- seq(as.Date("2014-01-01"), Sys.Date(), 1)
x <- sample(sampdates, 100, rep=TRUE)
epicurve.dates(x)
Run Code Online (Sandbox Code Playgroud)
这是结果情节:
什么都没有绘制,如果我们查看epicurve.dates
代码,我们可以看到问题发生时,它试图将日期向量编码为一个因素.只有NAs
生产.
format <- "%Y-%m-%d"; before <- after <- 7
dates0 <- as.Date(x, format = format)
min.date <- min(dates0, na.rm = TRUE) - before
max.date <- max(dates0, na.rm = TRUE) + after
cdates <- seq(min.date, max.date, by = 1)
> factor(dates0, levels = cdates)
[1] <NA> <NA> <NA> <NA> <NA> <NA> <NA> <NA> <NA> <NA> <NA> <NA> <NA> <NA> …
Run Code Online (Sandbox Code Playgroud) 我想将data.table中的一组列修改为因子.如果我事先知道列的名称,我认为这将是直截了当的.
library(data.table)
dt1 <- data.table(a = (1:4), b = rep(c('a','b')), c = rep(c(0,1)))
dt1[,class(b)]
dt1[,b:=factor(b)]
dt1[,class(b)]
Run Code Online (Sandbox Code Playgroud)
但我没有,而是有一个变量名列表
vars.factors <- c('b','c')
Run Code Online (Sandbox Code Playgroud)
我可以毫无问题地将因子函数应用于它们......
lapply(vars.factors, function(x) dt1[,class(get(x))])
lapply(vars.factors, function(x) dt1[,factor(get(x))])
lapply(vars.factors, function(x) dt1[,factor(get(x))])
Run Code Online (Sandbox Code Playgroud)
但我不知道如何重新分配或更新数据表中的原始列.
这失败了......
lapply(vars.factors, function(x) dt1[,x:=factor(get(x))])
# Error in get(x) : invalid first argument
Run Code Online (Sandbox Code Playgroud)
就像这样......
lapply(vars.factors, function(x) dt1[,get(x):=factor(get(x))])
# Error in get(x) : object 'b' not found
Run Code Online (Sandbox Code Playgroud)
NB.我试着在这里提出的答案没有任何运气.
r ×10
r-factor ×10
dataframe ×2
analysis ×1
correlation ×1
data.table ×1
if-statement ×1
numeric ×1