R-如果对列表应用了else
我是R新手,所以可能有些概念不完全正确。。。 我在列表中读取了一组文件(此处仅显示了每个文件的前3行):R-如果对列表应用了else,r,list,if-statement,apply,R,List,If Statement,Apply,我是R新手,所以可能有些概念不完全正确。。。 我在列表中读取了一组文件(此处仅显示了每个文件的前3行): myfiles也许这有帮助 lapply(myfiles,within, V3 <- ifelse(V2 >50, V3, NA)) #[[1]] # V1 V2 V3 #1 10001 33 NA #2 30001 65 0.0991478 #3 50001 54 0.1564400 #[[2]] # V1 V2
myfiles也许这有帮助
lapply(myfiles,within, V3 <- ifelse(V2 >50, V3, NA))
#[[1]]
# V1 V2 V3
#1 10001 33 NA
#2 30001 65 0.0991478
#3 50001 54 0.1564400
#[[2]]
# V1 V2 V3
#1 10001 62 0.085526
#2 30001 74 0.153664
#3 50001 71 0.102096
#[[3]]
# V1 V2 V3
#1 10001 49 NA
#2 30001 65 0.169615
#3 50001 61 0.070896
或者正如@Richie Cotton所提到的,您也可以使用rbindlist
将数据集绑定在一起,然后一步完成操作
library(tools)
dt1 <- rbindlist(lapply(files, function(x)
fread(x)[,id:= basename(file_path_sans_ext(x))] ))[V2<=50, V3:=NA]
dt1
# V1 V2 V3 id
#1: 10001 33 NA tab1
#2: 30001 65 0.0991478 tab1
#3: 50001 54 0.1564400 tab1
#4: 10001 62 0.0855260 tab2
#5: 30001 74 0.1536640 tab2
#6: 50001 71 0.1020960 tab2
#7: 10001 49 NA tab3
#8: 30001 65 0.1696150 tab3
#9: 50001 61 0.0708960 tab3
库(工具)
dt1您可以在
中使用lappy
和transform
/。有三种可能性:
- a)
ifelse
lapply(myfiles, transform, V3 = ifelse(V2 > 50, V3, NA))
- b)数学运算符(可能更有效)
这似乎比应该的更难,因为您使用的是数据帧列表,而不是单个数据帧。您可以使用dplyr
中的rbind\u all
将所有数据帧合并为一个数据帧
library(dplyr)
# Some variable renaming for clarity:
# myfiles now refers to the file names; mydata now contains the data
myfiles <- list.files(pattern="tab", full.names=TRUE)
mydata <- lapply(myfiles, read.table, skip="#")
# Get the number of rows in each data frame
n_rows <- vapply(mydata, nrow, integer(1))
# Combine the list of data frames into a single data frame
all_mydata <- rbind_all(mydata)
# Add an identifier to see which data frame the row came from.
all_mydata$file <- rep(myfiles, each = n_rows)
# Now update column 3
is.na(all_mydata$V3) <- all_mydata$V2 < 50
库(dplyr)
#为清晰起见,有些变量重命名:
#myfiles现在指的是文件名;mydata现在包含这些数据
myfiles尝试为每个df添加一个id列并将它们绑定在一起:
for(i in 1:3) myfiles[[i]]$id = i
ddf = myfiles[[1]]
for(i in 2:3) ddf = rbind(ddf, myfiles[[i]])
然后在复合df上应用更改并再次拆分:
ddf$V3 = ifelse(ddf$V2>50, ddf$V3, NA)
myfiles = lapply(split(ddf, ddf$id), function(x) x[1:3])
myfiles
$`1`
V1 V2 V3
1 10001 33 NA
2 30001 65 0.0991478
3 50001 54 0.1564400
$`2`
V1 V2 V3
11 10001 62 0.085526
21 30001 74 0.153664
31 50001 71 0.102096
$`3`
V1 V2 V3
12 10001 49 NA
22 30001 65 0.169615
32 50001 61 0.070896
如果要替换V3
列,只需修改函数:lappy(myfiles,function(x){x$V350,x$V3,NA);x})
。如果要添加另一个名为newcolumnname
的列,只需在上述命令中将x$V3
替换为x$newcolumnname
。是否替换V2
或V3
中的值?我想根据V2上的条件替换V3中的值这3种可能性的处理时间是否有任何首选项?我选择transform+ifelse(a),因为它对我来说更具可读性。Thanks@PedroA我更喜欢第三种解决方案。ifelse
版本显然是最低效的版本。为什么“@rnso”必须使用=
而不是为什么“=”在这里不起作用:lappy(myfiles,within,V3=ifelse(V2>50,V3,NA))?它抛出错误:eval(expr、envir、enclose)中的错误:缺少参数,没有default@rnso也许这个链接提供了一些想法
lapply(myfiles, transform, V3 = NA ^ (V2 <= 50) * V3)
lapply(myfiles, within, is.na(V3) <- V2 < 50)
[[1]]
V1 V2 V3
1 10001 33 NA
2 30001 65 0.0991478
3 50001 54 0.1564400
[[2]]
V1 V2 V3
1 10001 62 0.085526
2 30001 74 0.153664
3 50001 71 0.102096
[[3]]
V1 V2 V3
1 10001 49 NA
2 30001 65 0.169615
3 50001 61 0.070896
library(dplyr)
# Some variable renaming for clarity:
# myfiles now refers to the file names; mydata now contains the data
myfiles <- list.files(pattern="tab", full.names=TRUE)
mydata <- lapply(myfiles, read.table, skip="#")
# Get the number of rows in each data frame
n_rows <- vapply(mydata, nrow, integer(1))
# Combine the list of data frames into a single data frame
all_mydata <- rbind_all(mydata)
# Add an identifier to see which data frame the row came from.
all_mydata$file <- rep(myfiles, each = n_rows)
# Now update column 3
is.na(all_mydata$V3) <- all_mydata$V2 < 50
for(i in 1:3) myfiles[[i]]$id = i
ddf = myfiles[[1]]
for(i in 2:3) ddf = rbind(ddf, myfiles[[i]])
ddf$V3 = ifelse(ddf$V2>50, ddf$V3, NA)
myfiles = lapply(split(ddf, ddf$id), function(x) x[1:3])
myfiles
$`1`
V1 V2 V3
1 10001 33 NA
2 30001 65 0.0991478
3 50001 54 0.1564400
$`2`
V1 V2 V3
11 10001 62 0.085526
21 30001 74 0.153664
31 50001 71 0.102096
$`3`
V1 V2 V3
12 10001 49 NA
22 30001 65 0.169615
32 50001 61 0.070896