You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何从dataframe选取多变量生成新dataframe?操作报错求助

Fix: Convert Selected Variable Names to a New DataFrame

Hey there! I see where the confusion is—let's get this sorted out quickly.

The select.list() function doesn't return a dataframe directly. Instead, it gives you a character vector of the variable names you picked. That's why your subsequent operations threw errors—you were trying to use a list of names like a full dataframe!

Here's how to turn those selected names into the new dataframe you need:

Step 1: Confirm what LoanVars actually is

First, let's double-check the type of object LoanVars is. Run this line:

class(LoanVars)

You should see output like [1] "character"—that confirms it's just a vector of variable names, not a dataframe.

Step 2: Create your new dataframe

You have two easy ways to extract the selected columns from your original loanData dataframe:

Option 1: Base R Syntax

Use bracket indexing to pull the columns matching your selected names:

newLoanData <- loanData[, LoanVars]

Option 2: Tidyverse (dplyr) Syntax

If you prefer using the tidyverse, use select() with all_of() (this ensures dplyr recognizes your character vector as column names):

library(dplyr)
newLoanData <- loanData %>% select(all_of(LoanVars))

Step 3: Verify your new dataframe

To make sure everything worked, run these checks:

# Check dimensions (should be 114000 rows × 25 columns)
dim(newLoanData)

# Check the variable names match what you selected
names(newLoanData)

# Preview the first few rows
head(newLoanData)

Now newLoanData is the full dataframe you were expecting, with only the 25 variables you chose—you can use it for all your subsequent analysis!

内容的提问来源于stack exchange,提问作者Ti Em

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.20 12:17:38