ata.frame.

ata.frame.,r,dataframe,r-faq,R,Dataframe,R Faq,发生在我身上好几次。使用data.table包。当您只有一列需要引用时。使用其中一个 DT[[x]] 或 当有两列或更多列要引用时,请确保使用: DT[,..x] x可以是另一个data.frame中的字符串。NSE在这里是什么意思?@discipulus非标准评估;它用于使用延迟表达式来动态构建带有字符串的代码,而不是硬编码。更多信息请参见此处:NSE=非标准评估NSE在此处的含义是什么?@discipulus非标准评估;它用于使用延迟表达式来动态构建带有字符串的代码,而不是硬编码。参见此

发生在我身上好几次。使用data.table包。当您只有一列需要引用时。使用其中一个

DT[[x]]

当有两列或更多列要引用时,请确保使用:

DT[,..x]

x可以是另一个data.frame中的字符串。

NSE在这里是什么意思?@discipulus非标准评估;它用于使用延迟表达式来动态构建带有字符串的代码,而不是硬编码。更多信息请参见此处:NSE=非标准评估NSE在此处的含义是什么?@discipulus非标准评估;它用于使用延迟表达式来动态构建带有字符串的代码,而不是硬编码。参见此处了解更多信息:NSE=非标准评估我不知道为什么会投反对票,但它有效且简单,而不是编写复杂函数我不知道为什么会投反对票,但它有效且简单,而不是编写复杂函数这一情况在此后几年发生了变化吗?这种情况在此后几年发生了变化吗?
for(x in seq_along(cols)) {
   value <- mtcars[ order(mtcars$cols[x]), ]
}
set.seed(1)
dat <- data.frame(var1=round(rnorm(10)),
                   var2=round(rnorm(10)),
                   var3=round(rnorm(10)))
param <- paste0("var",1:3)
dat
#   var1 var2 var3
#1    -1    2    1
#2     0    0    1
#3    -1   -1    0
#4     2   -2   -2
#5     0    1    1
#6    -1    0    0
#7     0    0    0
#8     1    1   -1
#9     1    1    0
#10    0    1    0

for(p in rev(param)){
   dat <- dat[order(dat[,p]),]
 }
dat
#   var1 var2 var3
#3    -1   -1    0
#6    -1    0    0
#1    -1    2    1
#7     0    0    0
#2     0    0    1
#10    0    1    0
#5     0    1    1
#8     1    1   -1
#9     1    1    0
#4     2   -2   -2
`$`(df , V1)
`$`(df , "V1")
`$`(df , paste0("V1") )
var <- "mpg"
#Doesn't work
mtcars$var
#These both work, but note that what they return is different
# the first is a vector, the second is a data.frame
mtcars[[var]]
mtcars[var]
#  set seed for reproducibility
set.seed(123)
df <- data.frame( col1 = sample(5,10,repl=T) , col2 = sample(5,10,repl=T) , col3 = sample(5,10,repl=T) )

#  We want to sort by 'col3' then by 'col1'
sort_list <- c("col3","col1")

#  Use 'do.call' to call order. Seccond argument in do.call is a list of arguments
#  to pass to the first argument, in this case 'order'.
#  Since  a data.frame is really a list, we just subset the data.frame
#  according to the columns we want to sort in, in that order
df[ do.call( order , df[ , match( sort_list , names(df) ) ]  ) , ]

   col1 col2 col3
10    3    5    1
9     3    2    2
7     3    2    3
8     5    1    3
6     1    5    4
3     3    4    4
2     4    3    4
5     5    1    4
1     2    5    5
4     5    3    5
mtcars[do.call(order, mtcars[cols]), ]
library(dplyr)
mtcars %>% arrange(gear, desc(mpg))
sort_list <- c("gear", "desc(mpg)")
mtcars %>% arrange_(.dots = sort_list)
# Return the string name of the first name in names that is a column name in tbl
# else null
ChooseCorrectColumnName <- function(tbl, names) {
for(n in names) {
    if (n %in% colnames(tbl)) {
        return(n)
    }
}
return(null)
}

then...

cptcodefieldname = ChooseCorrectColumnName(file, c("CPT", "CPT.Code"))
icdcodefieldname = ChooseCorrectColumnName(file, c("ICD.10.CM.Code", "ICD10.Code"))

if (is.null(cptcodefieldname) || is.null(icdcodefieldname)) {
        print("Bad file column name")
}

# Here we use the hash table implementation where 
# we have a string key and list value so we need actual strings,
# not Factors
file[cptcodefieldname] = as.character(file[cptcodefieldname])
file[icdcodefieldname] = as.character(file[icdcodefieldname])
for (i in 1:length(file[cptcodefieldname])) {
    cpt_valid_icds[file[cptcodefieldname][i]] <<- unique(c(cpt_valid_icds[[file[cptcodefieldname][i]]], file[icdcodefieldname][i]))
}
A=mtcars[,which(conames(mtcars)==cols[1])]
#and then
colnames(mtcars)[A]=cols[1]
A$tmp=xyz
colnames(A)[colnames(A)=="tmp"]=x
> cols <- c("cyl", "am")
> get(cols[1], mtcars)
 [1] 6 6 4 6 8 6 8 4 4 6 6 8 8 8 8 8 8 4 4 4 4 8 8 8 8 4 4 4 8 6 8 4
   >study.df
   study   sample       collection_dt other_column
   1 DS-111 ES768098 2019-01-21:04:00:30         <NA>
   2 DS-111 ES768099 2018-12-20:08:00:30   some_value
   3 DS-111 ES768100                <NA>   some_value
> ## Selecting Columns in an Given order
> ## Create ColNames vector as per your Preference
> 
> selectCols <- c('study','collection_dt','sample')
> 
> ## Select data from Study.df with help of selection vector
> selectCols %>% select(.data=study.df,.)
   study       collection_dt   sample
1 DS-111 2019-01-21:04:00:30 ES768098
2 DS-111 2018-12-20:08:00:30 ES768099
3 DS-111                <NA> ES768100
> 
DT[[x]]
DT[,..x]
DT[,..x]