我正在尝试使用R提取在txt文件中找到的某些操作条件(即X> 10)并将其应用于R中的附加数据。因此,我将数据附加到以row = 40和column的csv格式的R中= 4(X1,X2,X3,X4)。
dat1=readLines("Patterns Interpreted.txt")
dat1
[1] "-------------------------" " Class 1 Vs. Class 0" "-------------------------"
[4] "Pattern 1" "X4 Less Than 141.5" ""
[7] "-------------------------" " Class 0 Vs. Class 1" "-------------------------"
[10] "Pattern 1" "X4 Greater Than 141.5" ""
dat2<-read.csv("LR.csv")
dat2
X1 X2 X3 X4 X5 X6 Y
1 2140 20640 30250 205 1732 99 4540
2 2016 20280 30010 195 1697 100 4315
3 1905 19860 29780 184 1662 97 4095
# For replacement in txt file
tx2 <- gsub(pattern = " Less Than ", replace = "<", x = dat1)
tx22 <- gsub(pattern = " Greater Than ", replace = ">", x = tx2)
tx22
[1] "-------------------------" " Class 1 Vs. Class 0" "-------------------------"
[4] "Pattern 1" "X4<141.5" ""
[7] "-------------------------" " Class 0 Vs. Class 1" "-------------------------"
[10] "Pattern 1" "X4>141.5" ""
请参阅txt,每种模式都有条件。我需要自动将这些条件提取为逻辑条件。换句话说,如果我有满足第一个条件的X4=120
,那么说一个新变量p=0
而不满足第二个条件,那么p=1
。
如何在R中执行此操作?
答案 0 :(得分:0)
您可以将eval
与str2lang
配合使用来实现此目的。
x<-5
str_cond<-"x<3"
condition<-str2lang(str_cond)
eval(condition) #prints FALSE
在您的示例中,将字符串条件分为左右两部分,并使用左部分作为dat1的字符串索引名称来获取所需的列。
str_cond<-"X4<141.5"
cond_parts<-strplit(str_cond,"<")[[1]] # split string
assign(cond_parts[1],dat1[,cond_parts[1]]) # create variable with name the left part and assign the column
condition<-str2lang(str_cond) # create language condition
eval(condition) # execute the language condition