我想用连续变量创建一个binned变量.我想要10个箱子,根据jenks分类的结果设置断点.如何将每个值分配给这10个箱中的一个?
# dataframe w/ values (AllwdAmt)
df <- structure(list(X = c(2078L, 2079L, 2080L, 2084L, 2085L, 2086L,
2087L, 2092L, 2093L, 2094L, 2095L, 4084L, 4085L, 4086L, 4087L,
4088L, 4089L, 4091L, 4092L, 4093L, 4094L, 4095L, 4096L, 4097L,
4098L, 4099L, 4727L, 4728L, 4733L, 4734L, 4739L, 4740L, 4741L,
4742L, 4743L, 4744L, 4745L, 4746L, 4747L, 4748L, 4749L, 4750L,
4751L, 4752L, 4753L, 4754L, 4755L, 4756L, 4757L, 4758L), AllwdAmt = c(34.66,
105.56, 105.56, 473.93, 108, 1669.23, 201.5, 62.67, 61.54, 601.28,
236.96, 108, 40.28, 29.32, 483.6, 236.96, 6072.4, 25.97, …Run Code Online (Sandbox Code Playgroud) 我正在根据另一个变量的字符串条件填充数据框的新变量.我收到以下错误消息:
Error in Source == "httpWWW.BGDAILYNEWS.COM" | Source == :
operations are possible only for numeric, logical or complex types
我的代码如下:
County <- ifelse(Source == 'httpWWW.BGDAILYNEWS.COM' | 'WWW.BGDAILYNEWS.COM', 'Warren',
ifelse(Source == 'httpWWW.HCLOCAL.COM' | 'WWW.HCLOCAL.COM', 'Henry',
ifelse(Source == 'httpWWW.KENTUCKY.COM' | 'WWW.KENTUCKY.COM', 'Fayette',
ifelse(Source == 'httpWWW.KENTUCKYNEWERA.COM' | 'WWW.KENTUCKYNEWERA.COM', 'Christian')
)))
我有数据框,其中日期列的格式为08/21/2000(m/d/Y)。
现在我想添加以季度格式显示日期的新列,即2000-Q3.
我安装了zoo包并使用了以下命令。但它正在赋予NA价值。
library(zoo)
mydf$var9=as.yearqtr("mydf$Order.Date","%m/%d/%Y").
Run Code Online (Sandbox Code Playgroud) 我希望在两个群体之间进行t检验(治疗组内外(分别在下面的样本数据中为1或0)),并且对于不同的研究,所有这些都位于同一数据框中.在下面的示例数据中,我想为1/0治疗组之间的所有变量(样本数据:Age,Dollars,DiseaseCnt)生成t检验.我希望按程序运行这些t检验,而不是跨群体运行.我有生成t检验的逻辑.但是,我需要帮助完成从功能中提取适当部分并创建易于消化的内容的最后一步.
最终,我想要的是:一个t-stats表,p值,执行t检验的变量,以及测试变量的程序.
DT<-data.frame(
Treated=sample(0:1,1000,replace=T)
,Program=c('Program A','Program B','Program C','Program D')
,Age=as.integer(rnorm(1000,mean=65,sd=15))
,Dollars=as.integer(rpois(1000,lambda=1000))
,DiseaseCnt=as.integer(rnorm(1000,mean=5,sd=2)) )
progs<-unique(DT$Program) # Pull program names
vars<-names(DT)[3:5] # pull variables to run t tests
test<-lapply(progs, function(i)
tt<-lapply(vars, function(j) {t.test( DT[DT$Treated==1 & DT$Program == i,names(DT)==j]
,DT[DT$Treated==0 & DT$Program == i,names(DT)==j]
,alternative = 'two.sided' )
list(j,tt$statistic,tt$p.value) }
) )
# nested lapply produces results in list format that can be binded, but complete output w/ both lapply's is erroneous
Run Code Online (Sandbox Code Playgroud) r ×4
binning ×1
continuous ×1
date-format ×1
extract ×1
if-statement ×1
intervals ×1
lapply ×1
string ×1