当我将因子转换为数字或整数时,我得到基础级别代码,而不是值作为数字.
f <- factor(sample(runif(5), 20, replace = TRUE))
## [1] 0.0248644019011408 0.0248644019011408 0.179684827337041
## [4] 0.0284090070053935 0.363644931698218 0.363644931698218
## [7] 0.179684827337041 0.249704354675487 0.249704354675487
## [10] 0.0248644019011408 0.249704354675487 0.0284090070053935
## [13] 0.179684827337041 0.0248644019011408 0.179684827337041
## [16] 0.363644931698218 0.249704354675487 0.363644931698218
## [19] 0.179684827337041 0.0284090070053935
## 5 Levels: 0.0248644019011408 0.0284090070053935 ... 0.363644931698218
as.numeric(f)
## [1] 1 1 3 2 5 5 3 4 4 1 4 2 3 1 3 5 4 5 3 2
as.integer(f)
## [1] 1 1 3 2 5 …Run Code Online (Sandbox Code Playgroud) 我似乎花了很多时间从文件,数据库或其他东西创建数据帧,然后将每列转换为我想要的类型(数字,因子,字符等).有没有办法一步完成,可能是通过给出一个类型的向量?
foo<-data.frame(x=c(1:10),
y=c("red", "red", "red", "blue", "blue",
"blue", "yellow", "yellow", "yellow",
"green"),
z=Sys.Date()+c(1:10))
foo$x<-as.character(foo$x)
foo$y<-as.character(foo$y)
foo$z<-as.numeric(foo$z)
Run Code Online (Sandbox Code Playgroud)
而不是最后三个命令,我想做类似的事情
foo<-convert.magic(foo, c(character, character, numeric))
Run Code Online (Sandbox Code Playgroud) 我需要创建一个data.frame,它将通过for循环的结果一次填充一行.它有45列:其中五列的名称是静态的,但其余部分在运行时从外部CSV文件读入(作为向量).我正在寻找一些类似的东西
goalsMenu <- read.csv("Phase 1 goalsmenu.csv", header = TRUE)
colHeads <- c("analysis","patient","date",as.vector(goalsMenu$Name),"CR")
output <- data.frame(colHeads)
Run Code Online (Sandbox Code Playgroud)
但是这会创建一个列名为colHeads的单列data.frame.
colHeads <- list("analysis","patient","date",as.vector(goalsMenu$Name),"CR")
Run Code Online (Sandbox Code Playgroud)
似乎是朝着正确方向迈出的一步,但我需要"压扁"它以创建所需的data.frame结构
你能告诉我吗?
R通常以"错误"格式理解数据框列,或者您只需要将列类从因子更改为字符以进行修改.我以前用以下方式更改了列类:
set.seed(1)
df <- data.frame(x = 1:10,
y = rep(1:2, 5),
k = rnorm(10, 5,2),
z = rep(c(2010, 2012, 2011, 2010, 1999), 2),
j = c(rep(c("a", "b", "c"), 3), "d"))
x <- c("y", "z")
for(i in 1:length(x)){
df[,x[i]] <- factor(df[,x[i]])}
Run Code Online (Sandbox Code Playgroud)
并回到数字:
x <- 1:5
for(i in 1:length(x)){
df[,x[i]] <- as.numeric(as.character(df[,x[i]]))} # Character cannot become numeric
Run Code Online (Sandbox Code Playgroud)
在我看来,也许有更好的方法做到这一点.我发现了这个问题,这正是我所需要的:
convert.magic <- function(obj,types){
out <- lapply(1:length(obj),FUN = function(i){FUN1 <-
switch(types[i],
character = as.character,
numeric = as.numeric,
factor = as.factor); FUN1(obj[,i])})
names(out) <- …Run Code Online (Sandbox Code Playgroud) 如果我的数据集有这样的列
ID Group Col_item_01 Col_item_02 Col_item 03
1 Blue 11.23 10.12 4.3
2 Green 10.21 18.24 5.9
4 Purple 4.23 7.64 15.97
Run Code Online (Sandbox Code Playgroud)
如何将所有以 , 开头的列转换Col_item_...为从字符键入数字?我知道我可以单独执行此操作, df1$Col_item_01 <- as.numeric(as.character(df1$Col_item_01)但我对使用 grep 或 grepl 或字符串函数来提取这些列并将其更改为数字的有效方法感兴趣Col_item_...。任何建议都非常感激。谢谢。
我想更改R数据框中的多个列的类,而不是一个接一个地更改它,也不要使用for循环(并注意此答案)。我可以用这两种方法中的任一种来做,但是它们感到笨拙。请注意,我不一定要更改每一列。
例如我有数据框mydf:
mydf <- data.frame("col1" = c(1, 2, 3),
"col2" = c("a", "b", "c"),
"col3" = c("a", "a", "b"), stringsAsFactors = FALSE)
Run Code Online (Sandbox Code Playgroud)
我想将第二列和第三列更改为类因子。(实际上,我想处理两个以上的专栏...)
我可以按自己喜欢的方式逐列进行操作,例如:
mydf$col2 <- as.factor(mydf$col2)
mydf[, 3] <- as.factor(mydf[,3])
Run Code Online (Sandbox Code Playgroud)
或者我可以使用for循环:
for (i in 2:3{
mydf[,i] <- as.factor(mydf[,i])
}
Run Code Online (Sandbox Code Playgroud)
这些工作,但感到笨拙和次优。
更好的主意?