我有一个包含许多列的数据集,我必须选择它们的一部分并重命名它们以便偶尔进行分析。我现在使用包select
中的dplyr
。但是,每次为许多属性进行设置都很复杂。有没有更好的方法来做到这一点?
例如,我使用数据集mtcars
> head(mtcars)
mpg cyl disp hp drat wt qsec vs am gear carb
Mazda RX4 21.0 6 160 110 3.90 2.620 16.46 0 1 4 4
Mazda RX4 Wag 21.0 6 160 110 3.90 2.875 17.02 0 1 4 4
Datsun 710 22.8 4 108 93 3.85 2.320 18.61 1 1 4 1
Hornet 4 Drive 21.4 6 258 110 3.08 3.215 19.44 1 0 3 1
Hornet Sportabout 18.7 8 360 175 3.15 3.440 17.02 0 0 3 2
Valiant 18.1 6 225 105 2.76 3.460 20.22 1 0 3 1
我想选择列:mpg
,cyl
,并将其重命名为x
,y
我现在使用:
> df <- mtcars %>% select(x=mpg, y=cyl)
> head(df)
x y
Mazda RX4 21.0 6
Mazda RX4 Wag 21.0 6
Datsun 710 22.8 4
Hornet 4 Drive 21.4 6
Hornet Sportabout 18.7 8
Valiant 18.1 6
它有效,但是当我经常更改括号中的参数时,这很麻烦。我希望使用列表来解决问题,但它不起作用。
例如,我希望创建一个属性列表以简化:
myselection <- c(
x = mpg,
y = cyl
)
df <- mtcars %>% select(myselection) # It is wrong!
但这是错误的,我怎样才能让它发挥作用?
答案 0 :(得分:7)
您需要非标准的评估(技术上,这是标准评估,正常的dplyr使用是NSE),请参阅vignette("nse")
:
library(dplyr)
dots <- list(x="mpg", y="cyl")
select_(mtcars, .dots = dots)
答案 1 :(得分:5)
基础R替代可能是
Sub selectFile()
Dim wbI As Workbook, wbO As Workbook
Dim wsI As Worksheet
Set wbI = ThisWorkbook
'sheet where you want to import the sheet in
Set wsI = wbI.Sheets("Sheet_Items")
ChDir (Environ("USERPROFILE") & "\Desktop")
'Select the file
Fname = Application.GetOpenFilename(filefilter:="Text Files (*.txt),*.txt", MultiSelect:=False)
' check if file is selected
If Fname = False Then
' MsgBox "No File Was Selected"
Exit Sub
End If
' delete the current content there
Sheets("Sheet_Items").Range("G3:AB50000").ClearContents
'Set wbO = Workbooks.Open(Fname)
'
'wbO.Sheets(1).Cells.Copy wsI.Cells
'
'wbO.Close SaveChanges:=False
Dim var As String
var = "TEXT;" & Fname
With ActiveSheet.QueryTables.Add(Connection:= _
var, Destination:=Range("$G$3") _
)
.Name = "Sample"
.FieldNames = True
.RowNumbers = False
.FillAdjacentFormulas = False
.PreserveFormatting = True
.RefreshOnFileOpen = False
.RefreshStyle = xlInsertDeleteCells
.SavePassword = False
.SaveData = True
.AdjustColumnWidth = True
.RefreshPeriod = 0
.TextFilePromptOnRefresh = False
.TextFilePlatform = 437
.TextFileStartRow = 1
.TextFileParseType = xlDelimited
.TextFileTextQualifier = xlTextQualifierDoubleQuote
.TextFileConsecutiveDelimiter = False
.TextFileTabDelimiter = True
.TextFileSemicolonDelimiter = True
.TextFileCommaDelimiter = False
.TextFileSpaceDelimiter = False
.TextFileColumnDataTypes = Array(1, 1, 1, 1, 1, 1)
.TextFileTrailingMinusNumbers = True
.Refresh BackgroundQuery:=False
End With
'You need to properly qualify the range for the Replace() method
Columns("S").Replace What:=".", _
Replacement:=",", _
LookAt:=xlPart, _
SearchOrder:=xlByRows, _
MatchCase:=False, _
SearchFormat:=False, _
ReplaceFormat:=False
Columns("T").Replace What:=".", _
Replacement:=",", _
LookAt:=xlPart, _
SearchOrder:=xlByRows, _
MatchCase:=False, _
SearchFormat:=False, _
ReplaceFormat:=False
'Range("S3:S100000") = Range("S3:S100000").Value
'Range("T3:T100000") = Range("T3:T100000").Value
'
'convert to number
'for further details on other available formats have a look at: http://stackoverflow.com/questions/20648149/what-are-numberformat-options-in-excel-vba
'Range("S:S").NumberFormat = "0"
'Range("T:T").NumberFormat = "0"
''get an emtpy cell
'Sheets("Sheet_Items").Range("ZZ100000").Copy
'Sheets("Sheet_Items").Range("S3:S100000").PasteSpecial , xlPasteSpecialOperationAdd
'
'Sheets("Sheet_Items").Range("ZZ100000").Copy
'Sheets("Sheet_Items").Range("T3:T100000").PasteSpecial , xlPasteSpecialOperationAdd
'select the cell B1
Sheets("Sheet_Items").Range("B1").Select
End Sub