与r中缺失或不匹配的组合(combination with missing or mismathes in r)
我必须创造独特的组合,同时允许一些不匹配。 以下是一个例子:
set.seed (1234) dataf <- data.frame (var1 = sample( c("A", "B", "-"),20, replace = T), var2 = sample( c("A"),20, replace = T), var3 = sample( c("B", "B", "B", "-"),20, replace = T), var4 = sample( c("A","A", "A", "-"),20, replace = T), var5 = sample( c("A", "B", "A", "A", "-"),20, replace = T) ) dataf
规则:
(1)生成独特的组合:
A B A B B - combination 1 A A A B B - combination 2 B B B A A - combination 3 so on ...
(2)允许一个(可以是n)不匹配来创建一个类别。 例如:
A B A B B A A A B B B A A B B B A B B B B A A B A
与不同变量的单一不匹配相同。
(3)“ - ”表示缺失值,可以被视为与匹配中的整数类似的方式,即允许一个不匹配。
A B A B B A - A B B A B A - B
但是,如果有两个缺失值,则组合声明为未知( - )
A B A B B A - A - B A B A - -
以下是针对上述数据的锻炼。
var1 var2 var3 var4 var5 comb 1 A A B - - - 2 B A B A A 1 3 B A B A A 1 4 B A B A A 1 5 - A B A A 1 6 B A B A - 1 7 A A B A B 2 8 A A B A B 2 9 B A B A A 1 10 B A - A - - 11 - A B A A 1 12 B A B - - - 13 A A B A A 2 14 - A B - A - 15 A A B A A 2 16 - A B A A 2 17 A A B A B 2 18 A A - A A 3 19 A A B A B 2 20 A A - A A 3
任何想法 ?
I have to create unique combination while allowing some mismatches. The following is an example:
set.seed (1234) dataf <- data.frame (var1 = sample( c("A", "B", "-"),20, replace = T), var2 = sample( c("A"),20, replace = T), var3 = sample( c("B", "B", "B", "-"),20, replace = T), var4 = sample( c("A","A", "A", "-"),20, replace = T), var5 = sample( c("A", "B", "A", "A", "-"),20, replace = T) ) dataf
Rules:
(1) Generate Unique combinations:
A B A B B - combination 1 A A A B B - combination 2 B B B A A - combination 3 so on ...
(2) Allow one (can be n) mismatch to create a category. For example:
A B A B B A A A B B B A A B B B A B B B B A A B A
are same as there a single mismatch at different variables.
(3) "-" indicates missing values, can be treated as similar way as integers in matching means that one mismatch allowed.
A B A B B A - A B B A B A - B
However if there are two missing values then combination is declared unknown (-)
A B A B B A - A - B A B A - -
The following is workout for the above data.
var1 var2 var3 var4 var5 comb 1 A A B - - - 2 B A B A A 1 3 B A B A A 1 4 B A B A A 1 5 - A B A A 1 6 B A B A - 1 7 A A B A B 2 8 A A B A B 2 9 B A B A A 1 10 B A - A - - 11 - A B A A 1 12 B A B - - - 13 A A B A A 2 14 - A B - A - 15 A A B A A 2 16 - A B A A 2 17 A A B A B 2 18 A A - A A 3 19 A A B A B 2 20 A A - A A 3
Any idea ?
原文:https://stackoverflow.com/questions/12760030
最满意答案
要获取包含Python文件的目录的完整路径,请在该文件中写入:
import os dir_path = os.path.dirname(os.path.realpath(__file__))
(注意,如果您已经使用
os.chdir()
来更改当前工作目录,那么上述的咒语将不起作用,因为__file__
常量的值相对于当前工作目录而不是由os.chdir()
更改os.chdir()
调用。)
获取当前工作目录的使用
import os cwd = os.getcwd()
以上使用的模块,常量和功能的文档参考:
os
和os.path
模块。__file__
常数os.path.realpath(path)
(返回“指定文件名的规范路径,消除路径中遇到的任何符号链接” )os.path.dirname(path)
(返回“路径名path
的目录名” )os.getcwd()
(返回“表示当前工作目录的字符串” )os.chdir(path)
( “将当前工作目录更改为path
” )To get the full path to the directory a Python file is contained in, write this in that file:
import os dir_path = os.path.dirname(os.path.realpath(__file__))
(Note that the incantation above won't work if you've already used
os.chdir()
to change your current working directory, since the value of the__file__
constant is relative to the current working directory and is not changed by anos.chdir()
call.)
To get the current working directory use
import os cwd = os.getcwd()
Documentation references for the modules, constants and functions used above:
- The
os
andos.path
modules.- The
__file__
constantos.path.realpath(path)
(returns "the canonical path of the specified filename, eliminating any symbolic links encountered in the path")os.path.dirname(path)
(returns "the directory name of pathnamepath
")os.getcwd()
(returns "a string representing the current working directory")os.chdir(path)
("change the current working directory topath
")
相关问答
更多-
dirname(__FILE__) . "/" . basename(__DIR__) 要么 dirname(__FILE__) dirname(__FILE__) . "/" . basename(__DIR__) or dirname(__FILE__)
-
你需要在__file__上调用os.path.realpath ,这样当__file__是没有路径的文件名时,你仍然得到dir路径: import os print(os.path.dirname(os.path.realpath(__file__))) You need to call os.path.realpath on __file__, so that when __file__ is a filename without the path you still get the dir path: ...
-
要获取包含Python文件的目录的完整路径,请在该文件中写入: import os dir_path = os.path.dirname(os.path.realpath(__file__)) (注意,如果您已经使用os.chdir()来更改当前工作目录,那么上述的咒语将不起作用,因为__file__常量的值相对于当前工作目录而不是由os.chdir()更改os.chdir()调用。) 获取当前工作目录的使用 import os cwd = os.getcwd() 以上使用的模块,常量和功能的文档参考 ...
-
如何查找当前目录并转到MySQL控制台中的目录?(How to find out current directory and go to a directory in MySQL console?)[2023-09-06]
这与MySQL命令行( >>> )或MySQL本身无关。 这只是说: 打开终端或外壳 。 在Windows中,这称为Command Prompt 。 将目录更改为文件所在的位置,使用cd (更改目录)命令执行此操作。 接下来,您只需键入gradle jettyRun 。 This has nothing to do with the MySQL command line (>>>), or MySQL itself. This is simply saying: Open your terminal or ... -
如何使用Groovy在当前目录中查找.zip文件?(How to find a .zip file in the current directory using Groovy?)[2024-02-18]
像这样的东西应该工作: filename=new File("directory").listFiles().find{it.name.endsWith(".zip")} 如果您不希望最后使用.zip,请减去它: filename=new File("directory").listFiles().find{it.name.endsWith(".zip")}.name - ".zip" (顺便说一下,第一个文件对象结束 - 你可以用它做任何你想做的事情。第二个结束时的字符串是没有.zip的名字) Som ... -
如何在当前目录中的所有* .txt文件上运行脚本?(How to run a script on all *.txt files in current directory? [duplicate])[2022-03-17]
使用glob.glob : import os, re import glob pattern = re.compile('word1(.*?)word3', flags=re.S) for file in glob.glob('*.txt'): with open(file) as fp: for result in pattern.findall(fp.read()): print result Use glob.glob: import os, re ... -
由于安全限制,在客户端计算机上运行的Javascript无法访问本地磁盘文件系统。 否则,人们可以使用Javascript以隐藏的形式上传文件。 如果您想访问客户端的磁盘文件系统,请在不同的角落寻找解决方案:从您的网页提供的嵌入式客户端应用程序,如Applet,Webstart,Silverlight或类似的东西。 如果您想访问服务器的磁盘文件系统,那么使用服务器端编程语言(如Java,PHP等)在服务器端角寻找解决方案,无论您的Web服务器当前正在使用/支持什么。 Javascript which run ...
-
请试试看它是否有帮助 loc=`dirname $BASH_SOURCE` Please try this to see if it helps loc=`dirname $BASH_SOURCE`
-
在index.php创建一个常量SITE_ROOT并在需要指定路径的任何地方使用它,如: require_once SITE_ROOT . '/path/from/siteroot/to/file.ext'; Create a constant SITE_ROOT in your index.php and use it everywhere you need to specify the path, like: require_once SITE_ROOT . '/path/from/siteroot/ ...
-
您可以使用basename和dirname : for f in /path/to/*/*.ext do command "$(dirname "$f")" "$(basename "$f")" done 使用.ext删除awk另一种方法: for f in /path/to/*/*.ext ;do echo "$f"|awk -F\/ '{a=$NF;$NF="";gsub(".ext","",a)}{print $0" "a}' OFS=\/ done You can use basen ...